OpenAL Sounds stop after lock or call

Member
Posts: 65
Joined: 2009.03
Post: #1
Hi all

I've just spotted a problem with a simple sound engine I've written. It uses OpenAL for sound effects and AVAudioPlayer for background music. The sound effects play fine when the game starts, but if a call comes in, or I lock the phone and then go back to the game, there is no sound.

Does anyone know how to fix this.

Cheers

MikeD
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #2
MikeD Wrote:I've just spotted a problem with a simple sound engine I've written. It uses OpenAL for sound effects and AVAudioPlayer for background music. The sound effects play fine when the game starts, but if a call comes in, or I lock the phone and then go back to the game, there is no sound.

Register your sound class with UIApplicationWillResignActiveNotification and UIApplicationDidBecomeActiveNotification. Those get sent when a phone call comes in and is finished.

Code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:@"UIApplicationWillResignActiveNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:@"UIApplicationDidBecomeActiveNotification" object:nil];

Then in the callbacks you basically shut down and restart the entire sound system. I free all my sounds, buffers, audio queues, audio session, etc. Then re-initialize them all after the call is over. Pain in the ass, but the iPhone sound system is so twitchy it's the only way (I've found) to keep the whole thing running.

Code:
- (void) applicationWillResignActive:(NSNotification *)notification
{
    [self pauseSoundSystem];
}

- (void) applicationDidBecomeActive:(NSNotification *)notification
{
    [self restartSoundSystem];
}

EDIT:

Oh, I forgot. There's also times where the sound system can get interrupted but your app *doesn't* change it's active state (so no notifications get sent). You'll want to register an Audio Session with an interruption callback to catch these.

Code:
AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);    // In your init code

void interruptionListenerCallback(void *inUserData, UInt32 interruptionState)
{
    MySoundClass        *controller = (MySoundClass *)inUserData;

    if (interruptionState == kAudioSessionBeginInterruption)
    {
        [controller pauseSoundSystem];
    }
    else if (interruptionState == kAudioSessionEndInterruption)
    {
        [controller restartSoundSystem];
    }
}
Quote this message in a reply
Member
Posts: 65
Joined: 2009.03
Post: #3
Thanks Bachus, that's great info which I'll try out.

I appreciate the quick response.

MikeD
Quote this message in a reply
Member
Posts: 65
Joined: 2009.03
Post: #4
Just in to finish this thread off, the problem was actually a bug with SDK Beta 5. Since GM the problem has gone away Smile

MikeD
Quote this message in a reply
Post Reply