代码之家  ›  专栏  ›  技术社区  ›  Moshe

对我的应用程序使用锁屏?

  •  3
  • Moshe  · 技术社区  · 14 年前

    我想让我的应用程序使用锁定屏幕上的音频按钮,同时多任务。 (是的,就像潘多拉) 我希望使用什么API?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Noah Witherspoon    14 年前

    看到了吗 Remote Control of Multimedia -beginReceivingRemoteControlEvents -remoteControlReceivedWithEvent: 方法。您将从锁屏控件和耳机点击器以及多任务抽屉左侧的控制按钮获得事件。要在应用程序不重要的情况下播放音频,还应该检查 this information 背景音频。

        2
  •  0
  •   mahboudz    9 年前

    从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;