【问题标题】:remoteControlReceivedWithEvent not call in a singleton classremoteControlReceivedWithEvent 不在单例类中调用
【发布时间】:2011-12-14 16:06:40
【问题描述】:

在我的应用程序中,我想实现音乐播放,就像 iPod 音乐库一样,可以播放背景和远程控制。

我的应用是:主页面有几个项目的表格视图,选择音乐项目将进入音乐视图并显示用户下载的音乐。然后在此页面中用户可以选择要播放的歌曲。

我在单例中创建了一个自定义播放器类,以便音乐仍然可以播放离开音乐视图页面的事件。现在我的问题是如何实现远程控制。我使用Apple guide 尝试过这种方式。当应用程序在音乐视图页面中然后进入后台时,它确实有效。

但是,如果该应用在其他页面并且正在播放音乐,则遥控器失败并且没有任何调用。

我的代码是这样的:


[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 sharedApplication] beginReceivingRemoteControlEvents];

现在当应用离开音乐查看页面时,我可以找到[FWAudioPlayer canBecomeFirstResponder] 被调用。然后我点击遥控器,[FWAudioPlayer remoteControlReceivedWithEvent:] 永远不会被调用。 然后我尝试在 AppDelegate 中接收远程控制事件。如果它可以在 AppDelegate 中接收事件,我可以分派事件处理并调用单例类。但是,它似乎永远不会在 AppDelegate 中被调用。

所以我想知道这里有什么问题。我的猜测是单例类FWAudioPlayer 不是真正的UIViewController,因为它不在应用程序的视图层次结构下。此外,当应用离开主页面等其他页面时,MainViewController 是第一响应者,FWAudioPlayer 永远无法获得远程事件。

如果我是对的,我怎样才能实现一个音乐播放器和iPod音乐一样的功能,尤其是有后台播放和远程控制?

如果我猜错了,如何让它(单例类)接收远程事件?

谢谢!!

【问题讨论】:

    标签: ios multitasking remote-control


    【解决方案1】:

    只要确定它是否可以成为第一响应者

    (BOOL)canBecomeFirstResponder {
        return YES;
    }
    

    【讨论】:

      【解决方案2】:

      我关注这个answer。 我将 UIWindow 子类化并自己调度事件。 但是我还是想知道为什么单例类收不到遥控器。

      如果有人告诉我,我会选择那个答案。

      我在 Apple 的事件处理中找到了答案,它为响应者清楚地描述了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多