iPhone HTML5 Audio tag not working

旧城冷巷雨未停 提交于 2019-12-10 10:09:25

问题


I have a problem with an audio tag not working on an iPonhe. It plays HTML5 Audio tags when tested on W3Schools website but will not work with my one below

 <audio autoplay ><source  src="audio/alarm1.mp3" type="audio/mpeg"></audio>

This code works fine on desktop and android browsers, just not ipod or iphone safari

EDITED

It would seem that the audio file is working ok, it just will not autoplay

Is there a way I could let the user press a button and there after play the music when ever it is required without user intervention?


回答1:


Autoplay does not work on iOS and android. They require some kind of user interaction to play an audio or a video.

If you play the audio on a user event(click/touchstart), it will play just fine.

If you don't have a user event on which it should play, What I usually do is bind a touchstart event on document and play and pause the audio on the first touchstart I get. Then whenever I need to play the audio, I play it again. (If the audio is played and paused once using a user interaction, you can play it again the next time without a user interaction).

//play and pause it once
document.addEventListener('touchstart', function () {
    document.getElementsByTagName('audio')[0].play();
    document.getElementsByTagName('audio')[0].pause();
});

//now play whenever you want it to play



回答2:


This is an option on the ui webview - starts with "media" auto play (check my post I answered this question with more details).

After the controller loads, load the html, then set the setting on your ui webview.

Check my previous post, I was searching for this recently and figured it out.



来源:https://stackoverflow.com/questions/17350924/iphone-html5-audio-tag-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!