javascript - Audio Context in iOS won't resume from suspend - Stack Overflow

I have a website that plays audio. It is most often run inside of a WebView in an iOS app, though it ca

I have a website that plays audio. It is most often run inside of a WebView in an iOS app, though it can be accessed directly. The audio is being played using an AudioContext. When the device goes to sleep, or the screen is put to sleep, the AudioContext will be suspended. I am listening to the statechange event and if the audio state changes to suspended or interrupted I will display a pause indicator when the user comes back to the app. Clicking the pause indicator triggers the following code:

async function resumeAudio() {
    if (context.state === SUSPENDED || context.state === INTERRUPTED) {
        try {
            console.log(`Audio context state is ${context.state}. Attempting to resume audio context.`);
            await context.resume();
            console.log(`Audio context resumed. State is now ${context.state}.`);
        } catch (e) {
            console.error('Error resuming audio context: ', e);
        }
    }
}

Sometimes when the context is suspended the resume call will work, sometimes it won't. When it fails to work the await context.resume() call never resolves.

Is there any explanation for why it gets stuck in suspend? The only reasons I've seem so far deal with needing the context.resume() call to be part of a user input, which is how I am doing it.


Edit: Realized just after posting that I fot to await the context.resume(). I edited the function to be async and am now awaiting the resume.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389501a4434728.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信