代码之家  ›  专栏  ›  技术社区  ›  Ali Shafai

在单触式中使用BeginReceivingRemoteControlEvents

  •  2
  • Ali Shafai  · 技术社区  · 16 年前

    我已经让我的应用程序在后台播放音乐,我还通过调用BeginReceivingRemoteControlEvents成功地使它成为媒体播放器。但是,从未调用RemoteControlReceived方法。目标C中的相同逻辑工作正常。感谢任何样品或指导方针。

    2 回复  |  直到 11 年前
        1
  •  0
  •   Dermot    14 年前

    您需要确保它被放置在第一响应者视图上,如果没有,则需要沿着响应者链向上传递事件,直到它到达您的远程事件。尝试想象远程控制事件与键盘上的按键相同。例如,如果应用程序集中在一个按钮上,而你按下了一些键盘键,什么都不会发生,因为按钮没有在听按键。 代码中也出现了类似的情况。尝试使用BeginReceivingRemoteControlEvents和方法override创建一个基本的、单视图的项目来接收事件(不记得它是什么,RemoteEventReceived()或类似的东西)。当您按下远程按钮时,这应该会被激发。

    (抱歉,我不能给出示例代码,现在不在Mac面前)

        2
  •  0
  •   mahboudz    11 年前

    您可能想尝试使用稍后的机制来监听远程控制事件。例如,如果您想收听耳机按钮:

        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)];
    

    您需要将此添加到文件的include区域:

    @import MediaPlayer;
    

    只有当你是一个在后台播放的音频应用程序时(也就是说,你必须在应用程序功能中设置该后台模式),这才在后台工作。

    推荐文章