Javascript音频在移动设备上不起作用 [英] Javascript audio isn't working on mobile devices

查看:77
本文介绍了Javascript音频在移动设备上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在桌面浏览器上可以正常工作(播放音乐).但是,当我尝试在任何移动设备上打开网站时,都无法正常工作.

The code below works fine on desktop browsers (plays music). But when I try to open the site on any mobile device it doesn't work.

var music = new Audio("mscs/gamemusic.mp3");

function playmusic() {
    music.controls = false;
    music.loop = false;
    music.autoplay = true;
    document.body.appendChild(music)
}

游戏开始时我在函数中使用它:

I use it in a function when game starts:

function createnewgame() {
    ...
    playmusic();
    ...
    ...
}

有人知道怎么了吗?

推荐答案

某些移动浏览器支持自动播放音频,但是ios,chrome和其他浏览器需要交互操作才能触发声音播放.您可以尝试使用Mutate属性... 在此处输入链接描述

Some mobile browsers support automatic playback of audio, but ios, chrome and others require interactive actions to trigger the sound playback.You can try working with the mute attribute...enter link description here

这篇关于Javascript音频在移动设备上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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