如何使用锁定屏幕上的媒体控制按钮使我的应用程序正常工作? [英] How do I make my app work with the media control buttons on lock screen?

查看:251
本文介绍了如何使用锁定屏幕上的媒体控制按钮使我的应用程序正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的iOS版本中,应用程序可以访问锁定屏幕上的媒体控制按钮,例如播放/暂停按钮:

In recent iOS versions apps have some kind of access to the media control buttons on the lock screen, like the Play/Pause button:

看起来按钮应该可以使用 MPMusicPlayerController 类,是吗?有没有办法从按钮获取原始事件?因为音乐播放器似乎只提供API来提供一堆 MPMediaItem s。如果我的应用程序是一个需要以不同方式处理按钮的收音机怎么办?

It looks like the buttons are supposed to work with the MPMusicPlayerController class, is that right? Is there a way to get the "raw" events from the buttons? Because the music player only seems to offer an API to supply a bunch of MPMediaItems. What if my app is for example a radio that needs to handle the buttons differently?

推荐答案

经过一番搜索我找到了这个相关问题让事情变得清晰。音乐播放器控制器类不是正确的轨道,诀窍是订阅控制器中的远程事件:

After a bit more searching I have found this related question that makes things clear. The music player controller class is not really the right track, the trick is to subscribe for remote events in your controller:

- (void) viewDidAppear: (BOOL) animated
{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) remoteControlReceivedWithEvent: (UIEvent*) event
{
    // see [event subtype] for details
}

这篇关于如何使用锁定屏幕上的媒体控制按钮使我的应用程序正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆