Ios 如何启用MPMediaItemArtwork下一个/上一个按钮?
我正在项目中使用Ios 如何启用MPMediaItemArtwork下一个/上一个按钮?,ios,objective-c,audio,avaudioplayer,Ios,Objective C,Audio,Avaudioplayer,我正在项目中使用AVAudioPlayer。 当应用程序进入后台或锁定设备时,应用程序使用AVAudioPlayer播放歌曲 我的问题是,当我使用AVAudioPlayer下载2首歌曲并播放歌曲并锁定设备时,将播放歌曲,但“下一步”和“上一步”按钮被禁用 我尝试使用以下代码启用按钮: [MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES; [MPRemoteCommandCenter shar
AVAudioPlayer
。
当应用程序进入后台或锁定设备时,应用程序使用AVAudioPlayer
播放歌曲
我的问题是,当我使用AVAudioPlayer
下载2首歌曲并播放歌曲并锁定设备时,将播放歌曲,但“下一步”和“上一步”按钮被禁用
我尝试使用以下代码启用按钮:
[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
并设置以下代码:
[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
然后设置mpmediatematwork
细节如下:
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
[songInfo setObject:@"song title" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
我还从应用程序功能中启用BackgroundMode
:
注意:当我关闭应用程序并再次运行,播放歌曲并锁定设备时,“下一步”和“上一步”按钮启用并工作,但第一次不工作。
那么我的问题是为什么不第一次启用下一个/上一个按钮
提前感谢。您能按如下所示为上一个和下一个轨迹对象设定目标吗
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
commandCenter.previousTrackCommand.enabled = YES;
commandCenter.nextTrackCommand.enabled = YES;
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"Next Track");
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"Previous Track");
return MPRemoteCommandHandlerStatusSuccess;
}];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//或
你的代码看起来很好 以下是我在Objective-C中的做法:
if(@available(iOS 11, *)) {
MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
}
我不知道功能,但希望这能帮到你:)感谢buddy的重播@AshishKakkad我已经做了这一步,但什么也没发生。谢谢你回答这个问题。但我已经完成了这个问题。使用相同的代码