2015-05-20 50 views
0

我正在制作处理音乐播放的iPhone音乐应用程序&暂停并保留其自己的播放列表。我试图让播放/暂停遥控功能(在耳塞和滑动屏幕上)在我的应用程序中播放音乐。MPRemoteCommandCenter事件处理程序从未触发

这里是我放入与音乐播放器相关的初始化方法(它是MPMusicPlayerController的一个实例,我确定通过调试调用这段代码)的代码,我将讨论这个问题I

[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; 
}]; 

我的问题:“M代码后,面对加入上面的代码后,当我按下滑屏幕上的播放/暂停按钮,当音乐在我的应用程序打,没有调试信息中显示出来日志,意味着事件处理程序块没有被触发。

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

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

回答

0

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

- (BOOL)canBecomeFirstResponder { return true; }

相关问题