I am working on an iPhone iOS4 that includes playing music from the iPod user library. I also want to keep track of which songs were played, and be able to change the song by accident, even in the background. So I installed the music player using:
[self setMusicPlayer: [MPMusicPlayerController iPodMusicPlayer]]
Now I want this application to continue to run and play music in the background, so I installed:
Required background modes: App plays audio
The problem I am facing is that my application loses control when it moves to the background (when applicationDidEnterBackground is called, i.e. on application switches). Since I use iPodMusicPlayer, music continues to play, but my application has no control and therefore cannot track or modify the song.
Now the Apple documentation states that your application should continue to run in the background using this required background mode tag, but mine is not. Is it because I'm using MPMusicPlayer? Is there any way around this? Any ideas?
PS. I am also trying to get remote locked and multi-tasking iPod controllers to work with my application. I use the code below, but remoteControlReceivedWithEvent is never called! Does it work with MPMusicPlayer? I only saw this with AVAudioPlayer.
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
NSLog(@"remoteControlReceivedWithEvent");
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"Play Pause");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"Next");
break;
default:
break;
}
}
- (BOOL)canBecomeFirstResponder {
NSLog(@"canBecomeFirstResponder");
return YES;
}
- (void) viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
source
share