iPod Touch Testing Begin/End Interruption

Discussion in 'iOS Programming' started by Darkroom, Jan 11, 2010.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    currently i'm trying to test device sound interruptions using the clock's alarm on my iPod touch. is there a better way?
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    alarm bug?

    also... is there a bug with the Clock app's alarm concerning interruption of sound?

    i am having difficulty reactivating sound after executing my -(void)endInterruption delegate method. the sound methods are firing but there is just no sound being produced from the device. to make sure i wasn't crazy i just tested having an alarm go off while playing a few other games i purchased from the app store and the same issue occurs.

    could this just be my device?

    [EDIT] it seems that sounds produced with AVAudioPlayer are not affected, only OpenAL.

    Code:
    void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) 
    	{
    	OpenALSoundManager *soundMgr = (OpenALSoundManager *) inUserData;
    
    	if (interruptionState == kAudioSessionBeginInterruption) 
    		{		
    		[soundMgr beginInterruption];
    		soundMgr.interrupted = YES;
    		}
    		 
    	else if ((interruptionState == kAudioSessionEndInterruption) && soundMgr.interrupted)
    		{
    		[soundMgr endInterruption];
    		soundMgr.interrupted = NO;
    		}
    	}
    	
    - (id)init
    	{
    	self = [super init];		
    	
    	if (self != nil) 
    		{		
    		OSStatus status = AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
    				
    		self.soundDictionary = [NSMutableDictionary dictionary];
    		self.soundEffectsVolume = 1.0;
    		self.backgroundMusicVolume = 1.0;		
    		[self endInterruption];
    		}
    	
    	return self;
    	}
    
    -(BOOL)startupOpenAL
    	{
    	ALCcontext *context = NULL;
    	ALCdevice *device = NULL;
    	device = alcOpenDevice(NULL);
    
    	if (!device) return NO;
    		
    	context = alcCreateContext(device, NULL);
    
    	if (!context) return NO;
    	
    	alcMakeContextCurrent(context);
    	return YES;	
    	}
    
    - (void)shutdownOpenAL
    	{
    	ALCcontext *context = NULL;
    	ALCdevice *device = NULL;
    	context = alcGetCurrentContext();
    	device = alcGetContextsDevice(context);
    	alcDestroyContext(context);
    	alcCloseDevice(device);
    	}
    	
    
    #pragma mark -
    #pragma mark Audio Session Management
    
    - (void)beginInterruption
    	{
    	[self stopBackgroundMusic];
    	[self clearAllSoundsFromMemory];
    	[self shutdownOpenAL];
    	
    	if (!self.isiPodAudioPlaying)
    		{
    		UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    		AudioSessionSetActive(YES);
    		}
    		else
    		{
    		UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
    		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    		AudioSessionSetActive(YES);
    		}
    	
    	AudioSessionSetActive(NO);
    	}
    
    - (void)endInterruption
    	{
    	[self setupAudioCategorySilenceIpod:NO];
    	[self startupOpenAL];	
    	
    	AudioSessionSetActive(YES);
    	}
    
    - (void)setupAudioCategorySilenceIpod:(BOOL)silenceIpod;
    	{
    	UInt32 audioIsAlreadyPlaying;
    	UInt32 propertySize = sizeof(audioIsAlreadyPlaying);
    	
    	OSStatus err = AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);	
    	
    	if (audioIsAlreadyPlaying && !silenceIpod)
    		{
    		self.isiPodAudioPlaying = YES;
    		UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
    		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);		
    		}
    		else
    		{
    		self.isiPodAudioPlaying = NO;
    		UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
    		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);		
    		}		
    	}
    
     

Share This Page