HTML5音频无法通过JavaScript播放,除非手动触发一次 [英] HTML5 Audio can't play through Javascript unless triggered manually once

查看:424
本文介绍了HTML5音频无法通过JavaScript播放,除非手动触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个小的声音文件使用标签和JavaScript来启动这自动播放。

I'm trying to get a small sound file to play automatically using an tag and javascript to initiate it.

<audio id="denied" preload="auto" controls="false">
    <source src="sound/denied.wav" />
 </audio>

然后通过JavaScript,在适当的时候:

And then through javascript, at the appropriate time:

$('#denied')[0].play()

工作正常铬我的桌面上。在安卓4.1.1,声音将不能播放,除非我打戏之前的JavaScript尝试播放HTML5的音频控制。

Works fine on Chrome on my desktop. In Android 4.1.1, the sound will not play, unless I hit "play" on the HTML5 audio controls before javascript attempts to play it.

除非用户在之前的JavaScript某一点开始其所以基本上Android浏览器(股票或海豚)将不会播放音频。这是意?有没有解决这个办法吗?

So basically the Android browser (stock or Dolphin) will not play the audio unless the user initiates it at some point before the javascript. Is this intended? Is there any way around this?

推荐答案

嗯,我的目的,这是我做的:

Well, for my purposes, here's what I did:

幸运的是,在用户可以触发该行为开始录音,他们必须点击一个按钮。我设置了单元的体积为0.0,并将它玩,当他们点击这个按钮。

Luckily, before the user can trigger the behavior to start audio, they have to click a button. I set the volume of the element to 0.0, and have it "play" when they click this button.

在声音静静地出场,我只是将音量设置属性回到1.0,它起着无需用户干预就好了。

After the sound is played silently, I simply set the volume property back to 1.0, and it plays without user intervention just fine.

这篇关于HTML5音频无法通过JavaScript播放,除非手动触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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