【问题标题】:MPRemoteCommandCenter Event Handlers Never TriggeredMPRemoteCommandCenter 事件处理程序从未触发
【发布时间】:2015-05-20 04:08:14
【问题描述】:

我正在制作一个 iPhone 音乐应用程序,它可以处理音乐播放和暂停,并维护自己的播放列表。我正在尝试使播放/暂停遥控器功能(在耳塞和上滑屏幕上)适用于我的应用中的音乐播放。

下面是我在音乐播放器相关的初始化方法中放的代码(是MPMusicPlayerController的一个实例,通过调试确定这块代码被调用了),我说一下我遇到的问题m 面对代码后:

[self.musicController beginGeneratingPlaybackNotifications];

// listen to remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];    

if ([self canBecomeFirstResponder]) {
    NSLog(@"Can be first responder");
    [self becomeFirstResponder];
    NSLog([NSString stringWithFormat: @"is first responder now? %@", [self isFirstResponder] ? @"yes" : @"no"]);
} else {
    NSLog(@"Cannot be first responder");
}

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: toggle command");
    return nil;
}];

[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: play command");
    return nil;
}];

[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: pause command");
    return nil;
}];

我的问题:添加上述代码后,当我在我的应用程序中播放音乐时按下上滑屏幕上的播放/暂停按钮时,日志中没有显示调试消息,这意味着事件处理程序块是' t 触发了。

当我在我的应用程序中播放音乐时按下苹果耳塞上的播放/暂停按钮时,系统开始在我的 iOS 音乐库中播放随机音乐,并且它正在播放的歌曲不会出现在播放中我的应用程序列表,这意味着除了执行上述步骤外,没有合法的方式可以从我的应用程序播放这首歌。

注意:日志的输出为“不能成为第一响应者”。

【问题讨论】:

    标签: ios iphone audio mpmusicplayercontroller remote-control


    【解决方案1】:

    您应该启用视图(视图控制器)成为第一响应者

    - (BOOL)canBecomeFirstResponder { return true; }

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 2011-09-07
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多