RemoteControlReceivedWithEvent 不在单例类中调用
在我的应用程序中,我想实现像iPod音乐库一样的音乐播放,可以播放背景和远程控制。
我的应用程序是:主页中有几个项目的表格视图,选择音乐项目将进入音乐视图并显示用户下载的音乐。然后在此页面中用户可以选择要播放的歌曲。
我在单例中创建了一个自定义 Player 类,以便音乐仍然可以在离开音乐视图页面时播放事件。现在我的问题是如何实现远程控制。我尝试使用 Apple 指南。当应用程序位于音乐视图页面然后进入后台时,它确实有效。
但是,如果应用程序在其他页面并且正在播放音乐,则远程控制失败并且没有任何调用。
我的代码是这样的:
[self.navigationController pushViewController:musicViewController animated:YES];
MusicViewController 有一个单例播放器,就像:
@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
当应用程序离开音乐视图页面时,我在这里做了某事
- (void)viewWillDisappear:(BOOL)animated { FWAudioPlayer *fwaudioPlayer = [FWAudioPlayer sharedAudioPlayer]; [fwaudioPlayer becomeFirstResponder]; }
顺便说一句,我已经在 AppDelegate 中设置了:
[[UIApplication shareApplication] beginReceivingRemoteControlEvents];
当应用程序离开音乐视图页面时,我可以发现 [FWAudioPlayer canBecomeFirstResponder]
被调用。然后我单击远程控制,[FWAudioPlayer remoteControlReceivedWithEvent:]
永远不会被调用。 然后我尝试在AppDelegate中接收远程控制事件。如果它可以在 AppDelegate 中接收事件,我就可以调度事件处理并调用单例类。然而,它似乎永远不会在 AppDelegate 中被调用。
所以我想知道这里的问题是什么。我的猜测是单例类 FWAudioPlayer
并不是真正的 UIViewController
因为它不在视图层次结构下应用程序。此外,当应用程序离开到主页面等其他页面时,MainViewController
是第一响应者,FWAudioPlayer
永远无法获取远程事件。
如果我是对的,我怎样才能实现一个具有与iPod音乐相同功能的音乐播放器,特别是具有后台播放和远程控制功能?
如果我的猜测是错误的,如何让它(单例类)接收远程事件?
谢谢!!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遵循这个答案。
我对 UIWindow 进行子类化并自己调度事件。
但我还是想知道为什么单例类无法接收远程控制。
如果有人告诉我,我会选择这个答案。
我在 Apple 的事件处理中找到了答案,它为响应者清楚地描述了。
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.
只要确定它是否可以成为第一响应者
Just make sure if it can become first responder