2017-04-20 167 views
2

在touchstart后播放某些音频时出现问题。我有以下的代码,我在Chrome 57上运行这样做:在touchstart上播放音频

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta charset="utf-8"> 
 
\t \t <title>Play Media on user touch</title> 
 
\t </head> 
 
\t <body> 
 
\t \t <div id="play">Click me to play audio!</div> 
 
\t \t <script type="text/javascript"> 
 
\t \t \t document.querySelector('#play').addEventListener('touchstart', function() { 
 
           new Audio('http://techslides.com/demos/samples/sample.mp3').play()  \t \t \t \t 
 
\t \t \t }) 
 
\t \t </script> 
 
\t </body> 
 
</html>

当我打开这个我的移动设备和测试touchstart,我在控制台收到此错误:

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

Error: Uncaught (in promise) DOMException: play() can only be initiated by a user gesture.

touchstart是否被视为用户手势?这是故意的行为还是错误?

回答

-1

几个月前,我设置的镀铬标志 铬://标志/#禁止-手势要求换媒体播放为true 和touchstart事件起到铬工作正常。 不幸的是,这个标志在上次更新chrome后消失了。

现在,我正在通过在手机中使用firefox解决这个问题。这是一个理想的解决方案,但我没有找到更好的方法。 对我来说奇怪的是,chrome使用了其他的js的其他实现。