I am trying to create a site where user can select a local audio file from their PC or tablet and (without uploading the file to the server) play that file using HTML5 audio tag (user is supposed to click the button to play it, autoplay feature is not needed). I am not able to achieve this in Android version of Chrome despite the fact that this browser should support everything that is needed. In desktop version of Chrome it works.
I have tried different methods how to load that file:
via JavaScript FileReader - you can fiddle with it here: http://liveweave.com/2kOWoz
function handleFileSelect(evt) { var files = evt.target.files; // FileList object playFile(files[0]); } function playFile(file) { var freader = new FileReader(); freader.onload = function(e) { player.src = e.target.result; }; freader.readAsDataURL(file); } player = document.getElementById('player'); document.getElementById('files').addEventListener('change', handleFileSelect, false); document.getElementById('play').onclick = function(){ player.play(); };
via URL.createObjectURL - edit here: http://liveweave.com/4y84XV The second one is very similar to the first one - only playFile function is like this:
function playFile(file) { player.src = URL.createObjectURL(file); }
What is wrong with those examples? Is there any other way how to achieve the expected result? I would appreciate any hints or ideas. Thanks.