【问题标题】:Play audio on touchstart在 touchstart 上播放音频
【发布时间】:2017-04-20 20:00:08
【问题描述】:

在触摸启动后播放一些音频时出现问题。我有以下在 Chrome 57 中运行的代码:

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

当我在移动设备上打开它并测试 touchstart 时,我在控制台中收到此错误:

警告:无法在“HTMLMediaElement”上执行“播放”:API 只能 由用户手势启动。

Error: Uncaught (in promise) DOMException: play() can only be 由用户手势启动。

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

【问题讨论】:

    标签: javascript android google-chrome touch html5-audio


    【解决方案1】:

    几个月前,我设置了 chrome flag chrome://flags/#disable-gesture-requirement-for-media-playback 为 true 并在 chrome 中正常播放 touchstart 事件。 不幸的是,上次更新 chrome 后,此标志消失了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多