在某些移动浏览器上无法通过javascript播放音频 [英] Audio is unable to play via javascript on some mobile browsers

查看:170
本文介绍了在某些移动浏览器上无法通过javascript播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一件简单的事情.我希望在用户进入网页后10秒钟后播放一些音频.我使用了以下代码

I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage. I used the following code

var aud=new Audio("someAudio.mp3");
$(document).ready(function(){
setTimeout(function(){aud.play()}, 10000);
});

它在桌面浏览器上运行良好.但是,尽管可以在Firefox中运行,但某些音频无法在诸如Google Chrome之类的移动浏览器中播放.可能的原因是什么?如何解决?我看到了一些类似的问题,但没有找到合适的答案. 预先感谢

It is working perfectly fine on desktop browsers. However, the audio is not playing in some mobile browsers like Google Chrome though it is working in Firefox. What may be the possible reason for this and how to fix it? I saw some similar questions but didn't find a suitable answer. Thanks in advance

推荐答案

我正在尝试做一件简单的事情.我希望在用户进入网页后10秒后播放一些音频.

I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage.

除非有用户交互,否则您将无法.

You can't unless there has been user interaction.

处理某些元素的click事件.在该事件处理程序中,播放其他音频. (该音频可以是无声的!)从加载开始经过10秒钟后,如果用户触摸/单击了某项,并且您已经完成此操作,则应该可以播放音频文件.

Handle a click event for some element. In that event handler, play some other audio. (This audio can be silent!) After 10 seconds have passed from load, if the user has touched/clicked something, and you've done this, you should be able to play your audio file.

这篇关于在某些移动浏览器上无法通过javascript播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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