|
|

楼主 |
发表于 2022-6-6 18:00
|
显示全部楼层
代码:未对文件进行格式过滤
- <style type="text/css">
- .mama { margin: auto; width: fit-content; height: fit-content; position: relative; }
- .mama fieldset { margin-top: 10px; padding: 4px 10px; border: 1px solid #eee; background: #ccc linear-gradient(rgba(0,0,0,.1),rgba(0,0,0,.15)); box-shadow: 1px 1px 2px #444; }
- .mama fieldset legend { padding: 4px 8px; border: 1px solid #eee; background: inherit; }
- #file { display: none; }
- #aud { outline: none; }
- #audlist { color: black; }
- #audlist li { cursor: pointer; }
- </style>
- <div class="mama">
- <fieldset>
- <legend><label style="cursor: pointer;">请选择文件<input id="file" type="file" accept="audio/*" multiple="multiple" /></label></legend>
- <ol id="audlist"></ol>
- </fieldset>
- <p><br><audio id="aud" controls="controls" autoplay="autoplay"></audio></p>
- </div>
- <script language="javascript">
- let file = document.querySelector('#file'),
- aud = document.querySelector('#aud'),
- audlist = document.querySelector('#audlist');
- let filelist = [];
- let idx = 0;
- file.onchange = () => {
- let fileArr = file.files;
- if(fileArr.length) {
- audlist.innerHTML = '';
- filelist.length = 0;
- Array.from(fileArr).forEach((item,key) => {
- let ar = [item.name,URL.createObjectURL(item)];
- filelist.push(ar);
- let listr = '<li onclick="playselected(' + key + ')">' + item.name.split('.')[0] + '</li>';
- audlist.innerHTML += listr;
- })
- aud.src = filelist[0][1];
- setColor();
- }
- }
- aud.addEventListener('ended', () => {
- idx ++;
- if(idx >= filelist.length) idx = 0;
- aud.src = filelist[idx][1];
- setColor();
- })
- let setColor = () => {
- Array.from(audlist.children).forEach((li,key) => {
- key == idx ? li.style.color = 'black' : li.style.color = 'gray';
- })
- }
- let playselected = (id) => {
- idx = id;
- aud.src = filelist[idx][1];
- setColor();
- }
- </script>
复制代码
|
|