HTML5 音频:Android 上的 Chrome 不会自动播放歌曲,而 PC 上的 Chrome 会

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12587977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:07:21  来源:igfitidea点击:

HTML5 Audio: Chrome on Android doesn't automatically play song vs Chrome on PC does

androidgoogle-chrome

提问by Pritesh Desai

I've made an HTML5 iPod.

我制作了一个 HTML5 iPod。

You can try it here.

你可以在这里试试。

http://inventikasolutions.com/demo/iPod

http://inventikasolutions.com/demo/iPod

On a PC, while using Chrome. If I navigate to a song, it starts playing automatically. But while using Chrome on Android it doesn't play the song. I have to hit the play/pause button again to play the audio.

在 PC 上,同时使用 Chrome。如果我导航到一首歌,它会自动开始播放。但是在 Android 上使用 Chrome 时,它​​不会播放这首歌。我必须再次点击播放/暂停按钮才能播放音频。

Here is the code which runs, when you select the song to play:

这是当您选择要播放的歌曲时运行的代码:

audioPlayer.src=songurl[number];
audioPlayer.oncanplaythrough = "isAppLoaded";
audioPlayer.autoplay = "autoplay";
audioPlayer.addEventListener('ended',nextSong,false);
document.getElementById("player").appendChild(audioPlayer);

and here is the play/pause code.

这是播放/暂停代码。

        if (audioPlayer.paused)
        {
        audioPlayer.play();
        $("#pauseindicator").hide();
        $("#playindicator").show();
        }
        else
        {
        audioPlayer.pause();
        $("#pauseindicator").show();
        $("#playindicator").hide();
        }

Could it have some thing to do with the 'autoplay' variable? The default browser in Android plays the song immediately.

它可能与“自动播放”变量有关吗?Android 中的默认浏览器会立即播放歌曲。

Thanks.

谢谢。

采纳答案by muhammed basil

Please refer this link ...

请参考这个链接...

http://code.google.com/p/chromium/issues/detail?id=138132

http://code.google.com/p/chromium/issues/detail?id=138132

Chrome does not allow applications to play HTML5 audio without an explicit action by the user, similar to how it is handled by iOS, but differently than the stock Android browser handles it.

Chrome 不允许应用程序在没有用户明确操作的情况下播放 HTML5 音频,类似于 iOS 的处理方式,但与普通 Android 浏览器的处理方式不同。

Autoplay is not honored on android as it will cost data usage.

安卓系统不支持自动播放,因为它会消耗数据流量。

回答by Kamil Szadkowski

In case someone bumps into this - Android version of Chrome in fact blocks autoplay function, but you can change settings of the browser. To do this, you have to enter chrome://flagsand set Disable gesture requirement for media playbackto on. It's impossible to force this setting via web, but if you're writing a dedicated web app like I did, you can use it.

万一有人碰到这个 - Android 版的 Chrome 实际上会阻止自动播放功能,但您可以更改浏览器的设置。为此,您必须输入chrome://flags并将媒体播放的禁用手势要求设置为开启。通过 Web 强制执行此设置是不可能的,但是如果您像我一样编写专用的 Web 应用程序,则可以使用它。

回答by Stepan Yakovenko

Add create.js to your page:

将 create.js 添加到您的页面:

<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>

and then launch sound this way:

然后以这种方式启动声音:

<script>
    createjs.Sound.registerSound("./click.mp3", "x");
    setTimeout(function () {
        createjs.Sound.play("x");
    }, 10000)
</script>

Works for me on lastest Android.

在最新的 Android 上对我有用。

回答by Hemerson Varela

It looks like there is new way to active Chrome to autoplay audio and video

看起来有一种新方法可以激活 Chrome 来自动播放音频和视频

Go to chrome://flags

转到 chrome://flags

Set Autoplay policyto No user gesture is required

设置Autoplay policyNo user gesture is required

Chrome flags

铬标志

回答by Victor Jatobá

Update your Chrome to 29, this version now support WebAudio API and fixed many issues.

将您的 Chrome 更新到 29,此版本现在支持 WebAudio API 并修复了许多问题。

see: Chrome for Android Update

请参阅:Android 版 Chrome 更新

I'm using the SoundJS Lib and work very well in all browsers (only don't work well in Default Android Browser and Chrome with the Android version is less than 4.1).

我正在使用 SoundJS Lib 并且在所有浏览器中都运行良好(仅在 Android 版本低于 4.1 的默认 Android 浏览器和 Chrome 中运行不佳)。

See these examples: www.createjs.com/#!/SoundJS/demos/testSuite

查看这些示例:www.createjs.com/#!/SoundJS/demos/testSuite

Support for Web Audio API on Chrome for Android: code.google.com/p/chromium/issues/detail?id=166003

支持 Android 版 Chrome 上的网络音频 API:code.google.com/p/chromium/issues/detail?id=166003

SoundJS limitations: community.createjs.com/kb/faq/soundjs-faq

SoundJS 限制:community.createjs.com/kb/faq/soundjs-faq