在我的应用程序中使用锁屏?

发布于 2024-09-07 12:35:52 字数 66 浏览 4 评论 0原文

我想让我的应用程序在多任务处理时使用锁定屏幕上的音频按钮。 (是的,就像潘多拉一样。) 我想要使​​用什么 API?

I'd like to make my app use the audio buttons on the lock screen while multitasking.
(Yes, like Pandora.)
What API am I looking to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-09-14 12:35:52

请参阅 多媒体远程控制文档。基本上,您只需要在共享应用程序实例上调用 -beginReceivingRemoteControlEvents ,然后将某些内容(可能是您的主视图控制器)注册为第一响应者并实现 -remoteControlReceivedWithEvent: 方法在它上面。您将从锁屏控件和耳机答题器以及多任务抽屉左侧的控制按钮获取事件。要在应用程序不是最重要的情况下播放音频,您还应该查看 有关背景音频的此信息

See the Remote Control of Multimedia docs. Basically, you just need to call -beginReceivingRemoteControlEvents on your shared application instance, then register something (probably your main view controller) as the first responder and implement the -remoteControlReceivedWithEvent: method on it. You will get events both from the lock-screen controls and from the headset clicker, as well as the control buttons to the left of the multitasking drawer. To play audio while your application isn't foremost, you should also check out this information on background audio.

当梦初醒 2024-09-14 12:35:52

从 iOS 7 开始,现在变得更加容易。这是播放/暂停切换(耳机按钮)的示例。有关更多选项,请参阅 MPRemoteCommandCenter 和 MPRemoteCommand 的文档。

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

或者,如果您更喜欢使用方法而不是块:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

要停止:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

或:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

您需要将其添加到文件的包含区域:

@import MediaPlayer;

It is even easier now, as of iOS 7. Here's the example for the play/pause toggle (headset button). See the docs for MPRemoteCommandCenter and MPRemoteCommand for more options.

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

or, if you prefer to use a method instead of a block:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

To stop:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

or:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

You'll need to add this to the includes area of your file:

@import MediaPlayer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文