admin管理员组

文章数量:1335386

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.

本文标签: javascriptAudio Context in iOS won39t resume from suspendStack Overflow