admin管理员组文章数量:1391975
I have a web app that uses the HTML5 <audio>
tag and for some reason, while it works fine on Windows and Mac PCs, it doesn't work on iOS and Android.
Here's a relevant snippet of my code:
Javascript:
var audioElement = document.querySelector('#audioplayer');
var source = document.querySelector('#mp3');
source.src = tokObject._url;
audioElement.load();
audioElement.play();
HTML:
<center>
<audio id="audioplayer" style="width:480px;">
<source id="mp3" src="random-placeholder" type="audio/mp3" />
</audio>
</center>
I have a web app that uses the HTML5 <audio>
tag and for some reason, while it works fine on Windows and Mac PCs, it doesn't work on iOS and Android.
Here's a relevant snippet of my code:
Javascript:
var audioElement = document.querySelector('#audioplayer');
var source = document.querySelector('#mp3');
source.src = tokObject._url;
audioElement.load();
audioElement.play();
HTML:
<center>
<audio id="audioplayer" style="width:480px;">
<source id="mp3" src="random-placeholder" type="audio/mp3" />
</audio>
</center>
Share
Improve this question
edited Jun 29, 2016 at 21:27
Zach Saucier
26.1k12 gold badges98 silver badges159 bronze badges
asked Feb 14, 2015 at 11:39
Pavel ZagalskyPavel Zagalsky
1,6467 gold badges24 silver badges57 bronze badges
2
- what browser you tested the code with ? i it opera it will not work – Sky Walker Commented Feb 14, 2015 at 11:41
- Chrome and Safari on iOS and Android – Pavel Zagalsky Commented Feb 14, 2015 at 11:41
3 Answers
Reset to default 3You normally can't autoplay audio or video files on mobile devices, this is often a restriction by the OSes such as Android and iOS to stop sites from downloading huge media files and autoplaying them.
If such code is called from within a click or touch handler, it will probably work, but not the way you are currently doing it.
Also, the <center>
element has been deprecated and you shouldn't use it anymore.
Not sure if this helps but I did this instead and it worked for me. While I am not playing an MP3 I am getting a voice to prompt the user through a loop.
function speak(text) {
var msg = new SpeechSynthesisUtterance();
var voices = speechSynthesis.getVoices();
msg.voice = voices[2];
msg.voiceURI = 'native';
msg.volume = 1;
msg.rate = 1;
msg.pitch = 1;
msg.text = text;
msg.lang = 'en-US';
speechSynthesis.speak(msg);
}
// insert this bottom bit to call the function and it will read in the mobiles native voice what you type in ie....rest works on android have not tried iphone.
speak('rest');
Have you tried it doing this way :
var aud=new Audio(file.mp3);
aud.play();
本文标签: javascriptCan39t make HTML5 Audio Tag to work on mobile browsersStack Overflow
版权声明:本文标题:javascript - Can't make HTML5 Audio Tag to work on mobile browsers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744595983a2614780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论