In my app, I'd like to implement the music playing just like iPod music library which can play background and remote control.
My App is : a table view with several items in the main page, selecting the music item it will enter the music view and show the musics user downloaded.Then in this page user can select song to play.
I create a custom Player class in singleton so that the music can still play event leaving the Music view page. Now my problem is how to implement the remote control. I tried this way using Apple guide. It really works when the app is in the music view page and then go into the background.
However, if the app is in the other page and the music is playing, the remote control is failed and nothing is call.
My code is sth like:
[self.navigationController pushViewController:musicViewController animated:YES];
The MusicViewController has a singleton player, which is like:
@interface FWAudioPlayer : UIViewController// I also tried to subclass of UIResponder, and it didn't work either { NSUInteger currectIndex; NSMutableArray *_urlArray; NSMutableArray *randomArray; AVAudioPlayer *_player; id fwDelegate; } @property (nonatomic, retain) NSMutableArray *urlArray; @property (nonatomic, retain) NSMutableArray *randomArray; @property (nonatomic, retain) AVAudioPlayer *audioPlayer; @property (nonatomic, assign) id fwDelegate; @property (nonatomic, assign) NSUInteger currectIndex; @property (nonatomic, assign) BOOL shuffle; + (id)sharedAudioPlayerWithData:(NSData *)data error:(NSError **)error; + (id)sharedAudioPlayer; @end
When app is leaving the music view page, I did sth here
- (void)viewWillDisappear:(BOOL)animated { FWAudioPlayer *fwaudioPlayer = [FWAudioPlayer sharedAudioPlayer]; [fwaudioPlayer becomeFirstResponder]; }
By the way, I've already set in the AppDelegate:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Now when the app leaves the music view page, I can find [FWAudioPlayer canBecomeFirstResponder]
is called. Then I click the remote control, [FWAudioPlayer remoteControlReceivedWithEvent:]
is never called.
Then I tried to receive the remote control event in the AppDelegate. If it can receive the event in the AppDelegate and I can dispatch the event handling and call the singleton class. However, it seems it'll never be called in the AppDelegate.
So I'd like to know what is the problem here.My guess is that the singleton class FWAudioPlayer
is not a really UIViewController
as it is not under the view hierarchy of the app. Moreover, when app leaves to other pages like main page, the MainViewController
is the first responder and FWAudioPlayer
can never get the remote event.
If I'm right, how can I implement a music player with the same function like iPod music,especially having background playing and remote control?
If my guess is wrong, how to make it(the singleton class ) to receive the remote event?
Thanks!!
I follow this answer. I subclass the UIWindow and dispatch the event myself. But I still want to know why the singleton class can't receive the remote control.
If anyone tells me that, I'll choose that answer.
I find the answer in Event handling by Apple and it describes clearly for the responder.