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

时间:2011-12-29 20:44:57

标签: iphone objective-c ios ios5

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

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

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

1 个答案:

答案 0 :(得分:12)

查看Remote Control of Multimedia文档。

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

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

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

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

然后回复他们:

- (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;
        }
    }
}