【问题标题】:iPod Controls in Lockscreen for own App锁定屏幕中的 iPod 控件为自己的应用程序
【发布时间】:2011-12-29 20:44:57
【问题描述】:

如何为我自己的应用程序使用锁定屏幕 iPod 控件?

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

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

【问题讨论】:

    标签: iphone objective-c ios ios5


    【解决方案1】:

    查看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;
            }
        }
    }
    

    【讨论】:

    • 这里不明显的是 viewDidAppear 应该在 viewController 中声明,而 remoteControlReceivedWithEvent 应该在应用程序委托中声明。
    • 这不是必需的,但在我之前的示例中是正确的。我忘了从文档中传达一个微妙的观点:UIViewController 可以参与响应者链,但只有在覆盖-(BOOL)canBecomeFirstResponder 后才能返回YES。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多