7

我想实现以下内容,

  1. 应用程序正在后台运行音乐或视频(使用MPMoviePlayerController)。
  2. 用户双击主页按钮并转到显示播放控件的第一个屏幕(快退、播放或暂停、快进按钮)
  3. 用户单击快退或快进按钮。然后应用播放上一个或下一个音乐或视频。

第三步,我应该知道点击了哪个按钮。(我自然知道,当前正在播放的项目被暂停、停止……使用MPMoviePlayerPlaybackStateDidChangeNotification通知)。

我应该注册哪个通知?或者还有其他方法吗?

4

2 回答 2

6

我自己得到了答案。

那就是使用 UIApplication 的 beginReceivingRemoteControlEvents。

在适当的位置(如 viewWillAppear:) 放置以下代码

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

并且视图控制器应该实现以下返回 YES 的方法

- (BOOL)canBecomeFirstResponder {
    return YES; 
}

然后您可以通过以下方法接收遥控器事件。

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {

    if( event.type == UIEventTypeRemoteControl ) {
        NSLog(@"sub type: %d", event.subtype);
    }
}

而 event.subtype 如下,

typedef enum {
    // available in iPhone OS 3.0
    UIEventSubtypeNone                              = 0,

    // for UIEventTypeMotion, available in iPhone OS 3.0
    UIEventSubtypeMotionShake                       = 1,

    // for UIEventTypeRemoteControl, available in iPhone OS 4.0
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;
于 2010-08-30T06:38:48.510 回答
1

这可能是一个很晚的答案,但正如我所注意到的,关于音频播放和远程控制的 Q/As 并不多,所以我希望我的回答能帮助其他有同样问题的人:

我现在正在使用AVAudioPlayer,但- (void)remoteControlReceivedWithEvent:(UIEvent *)event不能与您使用的播放器类型有关。

要使锁定屏幕上的前进和后退按钮正常工作,请执行以下操作:

在视图控制器的viewDidLoad方法中添加以下代码:

//Make sure the system follows our playback status - to support the playback when the app enters the background mode.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

然后添加这些方法: viewDidAppear::(如果尚未实现)

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //Once the view has loaded then we can register to begin recieving controls and we can become the first responder
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

viewWillDisappear:(如果尚未实施)

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    //End recieving events
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

和:

//Make sure we can recieve remote control events
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl)
    {
        if (event.subtype == UIEventSubtypeRemoteControlPlay)
        {
            [self playAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlPause)
        {
            [self pauseAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
        {
            [self togglePlayPause];
        }

        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
        {
            [self rewindTheAudio]; //You must implement 15" rewinding in this method.
        }
        else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
        {
            [self fastForwardTheAudio]; //You must implement 15" fastforwarding in this method.
        }

    }
}

这在我的应用程序中运行良好,但是如果您希望能够在所有视图控制器中接收远程控制事件,那么您应该在AppDelegate.

笔记! 这段代码目前运行良好,但我看到另外两个子类型称为UIEventSubtypeRemoteControlEndSeekingBackwardand UIEventSubtypeRemoteControlEndSeekingBackward。我不确定它们是否必须实施,如果有人知道,请告诉我们。

于 2013-12-04T08:12:28.177 回答