phone锁定期间是否可以触发音频播放

Can audio be triggered to play during phone lock

我正在制作一个网络聊天应用程序,现在我需要在收到新消息时播放通知声音。

首先,我使用 howler.js 来播放声音。在 howler 中,我们可以开始播放声音,然后如果我们锁定屏幕或更改浏览器选项卡焦点,它会继续播放。 但是如果选项卡打开并且移动屏幕被锁定,它不会开始播放声音,如果用户在另一个选项卡上打开另一个选项卡上的应用程序,它也不会播放。

所以基本上我的问题是,是否可以在屏幕锁定时开始播放声音,或者当用户在另一个选项卡上时开始播放声音?

app.js

var notify_sound = new Howl({
    src: ['./assets/sounds/notify2.mp3'],
    volume: 1.0
});
.
.
.
.
.
.
.
.
if(type == "user"){
        if(userAddress == remoteAddress) {
            typing.html('').fadeOut('fast');
            messages.append('<li class="self"><p class="message">'+ obj.message + '</p><p class="time">' + time +'</p></li><p class="username_self">' + userAddress + '</p>');
        }
        else {
            typing.html('').fadeOut('fast');
            messages.append('<li class="other"><p class="message">'+ obj.message + '</p><p class="time">' + time +'</p></li><p class="username_other">' + userAddress + '</p>');

            notify_sound.play();
        }
    }

HTML5 音频和移动并不真正相处。 Howler.js(如果您使用的是 2.0)使用变通方法通过将事件处理程序附加到将触发播放的第一个触摸事件来获得音频 'autoplay'。如果视图未激活,则不会有触摸事件(即使是模拟事件)来触发播放。

来自文档:

Mobile Playback

By default, audio on iOS, Android, etc is locked until a sound is played within a user interaction, and then it plays normally the rest of the page session (Apple documentation). The default behavior of howler.js is to attempt to silently unlock audio playback by playing an empty buffer on the first touchend event. This behavior can be disabled by calling:

Howler.mobileAutoEnable = false;

如果没有初始互动,您无能为力。