In my iPhone application, I play music using AVAudioPlayer. when my application runs in the background and it also works for tht, I wrote this code
in AppDelegate on - application doneFinishLaunchingWithOptions
audioManager= [[AudioManager alloc] init]; audioManager.delegate=self; audioManager.soundFile=[[[NSBundle mainBundle] pathForResource:@"mysong" ofType:@"wav"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; [audioManager setFileForPlay]; [audioManager.player setNumberOfLoops:-1]; [audioManager.player setVolume:.5]; [audioManager play]; -(void) playerBeginInterruption:(AVAudioPlayer *)p{ [usdef setValue:@"START" forKey:@"LOCATIONMANAGER"]; [locationManager startUpdatingLocation]; [p pause]; // audioManager=nil; } -(void) playerBeginInterruptionInBackground:(AVAudioPlayer *)p{ [usdef setValue:@"START" forKey:@"LOCATIONMANAGER"]; [locationManager startUpdatingLocation]; [p pause]; //audioManager=nil; } -(void) playerEndInterruption:(AVAudioPlayer *)p{ NSLog(@"Inteerpt END"); [p prepareToPlay]; [p play]; [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"]; } -(void) playerEndInterruptionInBackgroud:(AVAudioPlayer *)p{ NSLog(@"Inteerpt END BG"); // [self performSelector:@selector(allocaudioManager)]; [p prepareToPlay]; [p play]; [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"]; // [locationManager stopUpdatingLocation]; }
I have now created saperate classes for an audio manager that passes delegate methods
in .h file
in .m file
@implementation AudioManager @synthesize delegate; @synthesize player; @synthesize inBackground; @synthesize soundFile; void RouteChangeListener(void *inClientData, AudioSessionPropertyID inID,UInt32 inDataSize, const void *inData); -(id) init { if((self=[super init])) { OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL); if (result) NSLog(@"Error initializing audio session! %ld", result); [[AVAudioSession sharedInstance] setDelegate: self]; NSError *setCategoryError = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError]; if (setCategoryError) NSLog(@"Error setting category! %@", setCategoryError); result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self); if (result) NSLog(@"Could not add property listener! %ld", result); return self; } return nil; } -(void) setFileForPlay{ self.player=nil; player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:self.soundFile] error:nil];//initWithContentsOfURL:[NSURL URLWithString:self.soundFile] error:nil]; if (self.player) { player.delegate = self; } } -(void) play{ [self.player play]; } -(void)pause{ [self.player pause]; NSLog(@"in Pause"); } -(void)stop{ [self.player stop]; NSLog(@"in stop"); } void RouteChangeListener(void *inClientData, AudioSessionPropertyID inID,UInt32 inDataSize, const void *inData){ AudioManager* This = (AudioManager*)inClientData; if (inID == kAudioSessionProperty_AudioRouteChange) { CFDictionaryRef routeDict = (CFDictionaryRef)inData; NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); int reason = [reasonValue intValue]; if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) { [This stop]; } } } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag { if (flag == NO) NSLog(@"Playback finished unsuccessfully"); [p setCurrentTime:0.]; if (inBackground) { [delegate playerDidFinishPlayingInBackground:p]; } else { [delegate playerDidFinishPlaying:p]; } } - (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error { NSLog(@"ERROR IN DECODE: %@\n", error); } // we will only get these notifications if playback was interrupted - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p { NSLog(@"Interruption begin "); // the object has already been paused, we just need to update UI if (inBackground) { [delegate playerBeginInterruptionInBackground:p]; } else { [delegate playerBeginInterruption:p]; } } - (void)audioPlayerEndInterruption:(AVAudioPlayer *)p withFlags:(NSUInteger)flags { if(inBackground) { [delegate playerEndInterruptionInBackgroud:p]; } else { [delegate playerEndInterruption:p]; } NSLog(@"Interruption ended. Resuming playback"); } - (void)registerForBackgroundNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setInBackgroundFlag) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearInBackgroundFlag) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)setInBackgroundFlag { inBackground = true; } - (void)clearInBackgroundFlag { inBackground = false; } @end
Now the problem is that I play any songs from the ipod library and stop the song and close the ipod library, my original avaudioplayer, which played before the ipos library started, does not resume, so this is not a song in playong format
what could be wrong?
is there a demo project or example code that can help me?
Please help and suggest.