gpt4 book ai didi

ios - MPRemoteCommandCenter 事件处理程序从未触发

转载 作者:行者123 更新时间:2023-12-03 02:02:29 26 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

您应该启用 View ( View Controller )成为第一响应者
- (BOOL)canBecomeFirstResponder {
return true;
}

关于ios - MPRemoteCommandCenter 事件处理程序从未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com