mattgallagher / AudioStreamer

A streaming audio player class (AudioStreamer) for Mac OS X and iPhone.
http://cocoawithlove.com
1.93k stars 553 forks source link

Interruption listener is broken, needs the following fix #26

Open genzeb opened 13 years ago

genzeb commented 13 years ago

As other have also discovered, the interruption handler behavior is buggy if audio is interrupted while paused. The method - (void)handleInterruptionChangeToState:(AudioQueuePropertyID)inInterruptionState performs a [self pause] both at being and end interruptions. That, however, is inconsistent with the way - (void) pause; is implemented, and that is really a toggle play/pause and not just a pause. So, say you've paused a stream and get a call. That will trigger an audio begin interruption event. Since the stream is already paused, the interruption handler will start playing (instead of remaining paused). When the interruption ends (say the call ends), the stream is paused (unless of course you managed to pause/stop the stream in order to have a conversation on the phone).

I don't believe this warrens a fork so I will let the wonderful Mr. Gallaher issue a fix. In the mean time, this is what I am doing (my version also plays audio in the background fyi):

- (void)handleInterruptionChangeToState:(AudioQueuePropertyID)inInterruptionState 
{
    if (inInterruptionState == kAudioSessionBeginInterruption)
    { 

        if ([self isPlaying]) {
            [self pause];

            pausedByInterruption = YES; 
        } 
    }
    else if (inInterruptionState == kAudioSessionEndInterruption) 
    {
        AudioSessionSetActive( true );

        if ([self isPaused] && pausedByInterruption) {
            [self pause]; // this is actually resume

            pausedByInterruption = NO; // this is redundant 
        }
    }
}

The variable pausedByInterruption is needed since we only want to resume playing after an interruption iff audio was paused by an interruption. So, the ivar pausedByInterruption needs to be set to NO in the following methods: pause, start, stop and initWithUrl.