Lockscreen中的iPod控件适用于自己的应用程序

Jea*_*ler 4 iphone objective-c ios ios5

如何在我自己的应用程序中使用锁屏iPod控件?

我尝试过MPNowPlayingInfoCenter,但如果我设置了信息,它就不会显示在任何地方; 不在锁屏上,也不在AppleTV上播放.

我使用AVPlayer播放我的音频文件.

Ryd*_*kay 12

请查看远程控制多媒体文档.

以下是如何在UIViewController子类中侦听远程控制事件.首先,让您的控制器参与响应者链,否则事件将被转发给应用程序代理:

- (BOOL)canBecomeFirstResponder
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

在适当的情况下,告诉应用程序开始接收事件并使控制器成为第一响应者:

// maybe not the best place but it works as an example
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

然后回复他们:

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

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playOrStop: nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self previousTrack: nil];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self nextTrack: nil];
                break;

            default:
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)