JavaScript Play Uploaded Audio

后端 未结 3 1502
不知归路
不知归路 2020-11-29 07:11

How do I make it so that when audio is uploaded it can be played? I used this code, but it didn\'t work.



        
3条回答
  •  迷失自我
    2020-11-29 07:46

    function handleFiles(event) {
    	var files = event.target.files;
    	$("#audio").attr("src", URL.createObjectURL(files[0]));
    	$("#player")[0].load();
        $("#player")[0].play();
    }
    
    document.getElementById("file").addEventListener("change", handleFiles, false);
    
    
    

    This is pretty much the same thing as the most upvoted answer by @Kaiido, but uses Jquery. Furthermore, you have to use .load(); to load the audio before you play it. So what this code does for you is you can upload any audio file, and it will automatically start playing. Thanks!

提交回复
热议问题