admin管理员组文章数量:1402779
I'm using a .mp3 file, the .mp3 file plays okay when viewed directly and also when embeded using the HTML5 audio tag, however when creating the HTML5 audio tag in JS it does not play! (very strange)
I do not have this issue in any other browser/device, for example Desktop - Chrome works perfectly.
sound = document.createElement('audio');
sound.setAttribute('src', 'sound.mp3');
sound.play();
I've tested sound.canPlayType('audio/mpeg') and this produces true (so it is supported).
Perhaps there's a bug in Android - Chrome? (it is the latest version)
I'm using a .mp3 file, the .mp3 file plays okay when viewed directly and also when embeded using the HTML5 audio tag, however when creating the HTML5 audio tag in JS it does not play! (very strange)
I do not have this issue in any other browser/device, for example Desktop - Chrome works perfectly.
sound = document.createElement('audio');
sound.setAttribute('src', 'sound.mp3');
sound.play();
I've tested sound.canPlayType('audio/mpeg') and this produces true (so it is supported).
Perhaps there's a bug in Android - Chrome? (it is the latest version)
Share Improve this question asked Nov 21, 2015 at 22:29 StoneStone 3431 gold badge5 silver badges12 bronze badges 18-
Or perhaps you should let the browser load the source before playing. If possible, create the
audio
element well in advance. Listen to thecanplay
orcanplaythrough
event fired by theaudio
element to know when it has enough data to begin playback. – Touffy Commented Nov 21, 2015 at 22:36 - 1 Maybe mobile Chrome does not let you trigger play programmatically? Are you doing this on a user-triggered event? Maybe also try adding the element to the DOM. – Alexander O'Mara Commented Nov 21, 2015 at 22:39
-
1
@AlexanderO'Mara I know. But it doesn't make any sense here, and OP has tested that hypothesis, too. If
audio
can't be played back programatically, then that's that (but please check my hypothesis too). You could try using the Web Audio API as an alternative. It's a bit more code but also has much more power. – Touffy Commented Nov 21, 2015 at 22:54 -
1
@magreenberg We already know that the audio plays back fine when the
audio
element is part of the HTML. So it can't be a network thing. – Touffy Commented Nov 21, 2015 at 22:57 - 2 Just found this, it's intended by google! :( – Matt Greenberg Commented Nov 21, 2015 at 23:03
4 Answers
Reset to default 4Looks like this is intended feature that spans more then just the Chrome browser. User interaction is required to get media elements to play.
Blink and WebKit have a setting for requiring a “user gesture” to play or pause an audio or video element, which is enabled in Opera for Android, Chrome for Android, the default Android browser, Safari for iOS and probably other browsers. This makes some sense, since mobile devices are used in public and in bed, where unsolicited sound from random Web sites could be a nuisance. Also, autoplaying video ads would waste bandwidth. Block Quote from 'blog.foolip'
Duplicate Threads from Other Users
Autoplay audio on mobile safari
How can I autoplay media in ios 4.2.1
Autoplay audio with ios 5 workaround?
Current Status
Developers have requested the deletion of 'mediaPlaybackRequiresUserGesture' which was reviewed and denied (for now). "We're going to gather some data about how users react to autoplaying videos in order to decide whether to keep this restriction."
Upon further inspection i found this...
"I misunderstood the oute of the discussion (removing mediaPlaybackRequiresUserGesture) surrounding this topic. We need to keep this code in order to not break google. while gathering data about this feature."
Google. relies on the feature being disabled, otherwise it breaks (they didn't say what it breaks).
Original Bug Report
Try appending it to the document body.
document.body.appendChild(sound);
Though it is possible that mobile devices will not automatically play the audio or videos. If you are targeting mobile devices, autoplaying is considered bad practice since it can consume bandwidth. So it may be worth considering adding controls.
sound.setAttribute('controls', 'true');
OK, well, now that we know it won't work with audio
, the only path left to you is to switch to the Web Audio API. You'll need to load the mp3 into an ArrayBuffer (e.g. using an XHR), then pass that to the decodeAudioData method, which gets you an Audio buffer that you can play back at will from an AudioBufferSourceNode.
Not every browser on every platform can play the mp3 audio format. Generally, as I would remend, you should provide two <source>
elements within your audio element, one providing the mp3 format, and another one providing the ogg vorbis format.
You can read more here: https://developer.mozilla/en-US/docs/Web/HTML/Supported_media_formats
本文标签: javascriptHTML5 audio tag does not work in AndroidChrome when created in JSStack Overflow
版权声明:本文标题:javascript - HTML5 audio tag does not work in Android - Chrome when created in JS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744282772a2598735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论