简体   繁体   English

如何将文件加载到 html5 音频标签中

[英]How to load a file into a html5 audio tag

How would a I load a audio file from a <input type="file"> tag into a audio tag?我如何将<input type="file">标签中的音频文件加载到音频标签中? I have tried :我试过了 :

<input type="file" id="file"></input>
<script>
var file = document.getElementById("file");
var audio = document.createElement("audio");
audio.src = file.value;
document.write(audio)
</script>

I believe it will satisfy your needs.我相信它会满足您的需求。 First, the file input needs to be bound via JavaScript or jQuery (if you prefer).首先,文件输入需要通过 JavaScript 或 jQuery(如果您愿意)进行绑定。 You can use Blob browser support您可以使用Blob浏览器支持

The following is a very basic example;下面是一个非常基本的例子;

<input type="file" id="file"></input>
<audio id="audio" controls autoplay></audio>

We bind the #file for changes using AddEventListener as below我们使用AddEventListener绑定#file以进行更改,如下所示

// Check for BlobURL support
var blob = window.URL || window.webkitURL;
    if (!blob) {
        console.log('Your browser does not support Blob URLs :(');
        return;           
    }

document.getElementById('file').addEventListener('change', function(event){

        consolePrint('change on input#file triggered');
        var file = this.files[0],
         fileURL = blob.createObjectURL(file);
        console.log(file);
        console.log('File name: '+file.name);
        console.log('File type: '+file.type);
        console.log('File BlobURL: '+ fileURL);
        document.getElementById('audio').src = fileURL;

});

Or the other hand, here's a nice and more interactive example I created或者另一方面,这是我创建的一个很好且更具交互性的示例

 <iframe style="height:600px;width:102.7%;margin:-10px;overflow:hidden;" src="//jsfiddle.net/adamazad/0oy5moph/embedded/result,js,html,css/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

 var wrap = document.getElementById("wrap"); var file = document.getElementById("file"); var audio = document.createElement("audio"); file.onchange = function() { audio.src = file.value; wrap.appendChild(audio); };
 <div id="wrap"> <input type="file" id="file"> </div>

This is to realize the train of thought, but this also cannot achieve, because url that the upload is not local url, You can discuss with me in the comments section这是实现思路,但是这个也实现不了,因为上传的url不是本地url,大家可以在评论区和我讨论

I have developed an es6 code to load a file into an Audio element with a Promise that catches errors and resolves on success我开发了一个 es6 代码来将文件加载到带有 Promise 的 Audio 元素中,该 Promise 可以捕获错误并在成功时解决

 function set_srcObject(element,f){ if ('srcObject' in element) { try{ element.srcObject = f; // this is the new way. only safary supports muliplt inputs, it is possible to put here media streams and files and blobs, but current new browsers support only media stream so need a fallback. } catch(e) { if (e.name != "TypeError") throw e; // Avoid using this in new browsers, as it is going away. element.src = URL.createObjectURL(f); } } else element.src = URL.createObjectURL(f); } function loadFile(audio, f) { var onloadeddata_resolve, onloadeddata_reject; // handle both events once function onloadeddata_ev(){ onloadeddata_resolve(); audio.removeEventListener('loadeddata',onloadeddata_ev); audio.removeEventListener('error',onerror_ev); } function onerror_ev(e){ onloadeddata_reject(e.srcElement.error); audio.removeEventListener('loadeddata',onloadeddata_ev); audio.removeEventListener('error',onerror_ev); } audio.addEventListener('loadeddata',onloadeddata_ev); audio.addEventListener('error',onerror_ev); const promise= new Promise( (resolve,reject) => { onloadeddata_resolve = resolve; onloadeddata_reject = reject} ); // inversion of control promise // try load it: try{ //audio.src = url; // = http://example.org/myfile.mp3 or .ogg set_srcObject(audio,f); audio.load(); } catch (e) { audio.removeEventListener('loadeddata',onloadeddata_ev); audio.removeEventListener('error',onerror_ev); onloadeddata_reject(e);} return promise; } //example: let audio = new Audio(); // create audio element audio.autoPlay=false; filefield.oninput=async function test() { try { const f = filefield.files[0]; // take first file await loadFile(audio, f); // load the file await audio.play(); } catch(e) { console.log(e.stack?e.stack:e) } } playpause.onclick = async () => { if( audio.paused) await audio.play(); else await audio.pause(); };
 <input type="file" id="filefield" multiple="multiple" accept="audio/mpeg, audio/mp4, audio/ogg"> <input id="playpause" value="play pause" type="button">

this solution was a part of a playlist player object I have developed https://jsfiddle.net/shimondoodkin/9jrzhd35/13/此解决方案是我开发的播放列表播放器对象的一部分https://jsfiddle.net/shimondoodkin/9jrzhd35/13/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM