您好亲爱的同事,我需要您的帮助。当我添加MPChangePlaybackPositionCommand时,我的所有控件(锁定屏幕上的“播放/暂停/倒退/下一首”曲目)已自动禁用。锁定屏幕上的“播放”滑块可完美工作,但我无法按任何一个控制按钮来说明为什么-我不知道。
我也试过这个:
[[MPRemoteCommandCenter sharedCommandCenter].playCommand setEnabled:YES];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand setEnabled:YES];
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand setEnabled:YES];
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand setEnabled:YES];
我的代码很简单:
MPChangePlaybackPositionCommand *changePlaybackPositionCommand = [[MPRemoteCommandCenter sharedCommandCenter] changePlaybackPositionCommand];
[changePlaybackPositionCommand addTarget:self action:@selector(onChangePlaybackPositionCommand:)];
- (MPRemoteCommandHandlerStatus) onChangePlaybackPositionCommand:
(MPChangePlaybackPositionCommandEvent *) event
{
[[[PlayerPlistController utilise]miniplayer] seekToTime:CMTimeMakeWithSeconds(event.positionTime, 1)];
NSLog(@"changePlaybackPosition to %f", event.positionTime);
return MPRemoteCommandHandlerStatusSuccess;
}
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
[songInfo setObject:NAME_TITLE forKey:MPMediaItemPropertyTitle];
[songInfo setObject:NAME_TITLE_SLOGON forKey:MPMediaItemPropertyAlbumTitle];
UIImage *image = [UIImage imageNamed:PLACEHOLDER_EMPTY];
MPMediaItemArtwork *imageArt = [[MPMediaItemArtwork alloc] initWithBoundsSize:image.size requestHandler:^UIImage* _Nonnull(CGSize aSize) { return image; }];
[songInfo setObject:imageArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
最佳答案
让我们以“播放/暂停”按钮为例。仅启用它是不够的。您还必须实现它(我为在Swift中编写代码而道歉;我懒得将其转换回Objective-C,但您肯定可以理解这一点):
let mprc = MPRemoteCommandCenter.shared()
mprc.playCommand.addTarget(self, action:#selector(doPlay))
mprc.pauseCommand.addTarget(self, action:#selector(doPause))
等等,以及实际的
doPlay
和doPause
实现。