无法执行对“HTMLMediaElement'玩':API只能由用户手势启动 [英] Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture

查看:5606
本文介绍了无法执行对“HTMLMediaElement'玩':API只能由用户手势启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个音乐播放页面,在这里我使用 SoundManager类2 AngularJs 的。
我使用远程API来获取歌曲的URL玩。我的增强角soundmanager2单击事件处理程序:

I'm making a music playing page, where I use SoundManager 2 for AngularJs. I'm using a remote API to get a song URL to play. I enhanced an angular-soundmanager2 click event handler :

element.bind('click', function () {
                    if (angular.isFunction(scope.loadFunction)) {
                        scope.loadFunction(scope.song, function () {
                            $log.debug('adding song to playlist');
                            addToPlaylist(scope.song.playDetails);

                        })
                    } else {
                        $log.debug('adding song to playlist');
                        addToPlaylist(scope.song);
                    }
                });

在哪里我加了一部分,调用 scope.loadFunction(歌曲,回调)键,这个功能后,加载它调用的回调给控制回歌曲网址角soundmanager2。

Where I added a part, that calls scope.loadFunction(song,callback) and after this function loads a song URL it calls a callback to give the control back to angular-soundmanager2.

问题是,在Android版Chrome浏览我得到一个错误:

The problem is that on chrome for android I get an error :

Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture.

如果一首歌曲从开始的URL,而不是使用异步加载它不会发生。

it doesn't happen if a song has a URL from the beginning and async loading isn't used.

对于它有什么解决办法?
谢谢

Are there any workarounds for it? Thanks

推荐答案

我曾与铬播放声音实验。原来,即使是用户操作(如点击),它等待1000毫秒,如果没有播放声音,它抛出上述异常后。在我的情况下,问题是从异步跟踪URL负载来了。

I had to experiment with Chrome playing sounds. It turned out that even after a user gesture (such as click) it waits for 1000ms and if no sound was played it throws the exception above. In my case the problem was coming from asynchronous track URL loading.

但是,这也证明,第一首曲目播放完毕后镀铬已经不为这个1000毫秒延迟照顾,你可以叫你想要的任何超时程序播放。

But it also turned out that after the first track is played chrome doesn't care anymore for this 1000ms delay and you can call play programmatically with any timeout you want.

因此​​,解决办法是第一次用户点击的轨道和负荷后所需曲目URL后玩微几乎为零秒长的静态资源静音的声音。

So the solution was to play a micro almost zero-second long muted sound from static resources after the very first time a user clicks on a track and after that load a desired track URL.

希望它能帮助,或是有人找到其他解决方案。

Hope it helps or someone finds other solutions.

这篇关于无法执行对“HTMLMediaElement'玩':API只能由用户手势启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆