#pragma 更新锁屏界面信息
- (void)updateLockScreenInfo {
if (!musicManager.player) {
return;
}
// 1.获取锁屏中心
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
// 初始化一个存放音乐信息的字典
NSMutableDictionary *playingInfoDict = [NSMutableDictionary dictionary];
// 2、设置歌曲名
[playingInfoDictsetObject:@"Avengers"
forKey:MPMediaItemPropertyTitle];
//专辑
[playingInfoDictsetObject:@"Avengers"
forKey:MPMediaItemPropertyAlbumTitle];
// 3、设置封面的图片
UIImage*image = [UIImageimageNamed:@"haveno"];
if(image) {
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[playingInfoDictsetObject:artworkforKey:MPMediaItemPropertyArtwork];
}
// 4、设置歌曲的时长和已经消耗的时间
NSNumber*playbackDuration =@(CMTimeGetSeconds(musicManager.player.currentItem.duration));
NSNumber*elapsedPlaybackTime =@(CMTimeGetSeconds(musicManager.currentItem.currentTime));
if(!playbackDuration || !elapsedPlaybackTime) {
return;
}
[playingInfoDictsetObject:playbackDuration
forKey:MPMediaItemPropertyPlaybackDuration];
[playingInfoDictsetObject:elapsedPlaybackTime
forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[playingInfoDictsetObject:@(musicManager.player.rate) forKey:MPNowPlayingInfoPropertyPlaybackRate];
//音乐信息赋值给获取锁屏中心的nowPlayingInfo属性
playingInfoCenter.nowPlayingInfo= playingInfoDict;
}
#pragma mark-- 远程控制
- (void)createRemoteCommandCenter {
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand*pauseCommand = [commandCenterpauseCommand];
[pauseCommandsetEnabled:YES];
[pauseCommandaddTarget:selfaction:@selector(remotePauseEvent)];
MPRemoteCommand*playCommand = [commandCenterplayCommand];
[playCommandsetEnabled:YES];
[playCommandaddTarget:selfaction:@selector(remotePlayEvent)];
MPRemoteCommand*nextCommand = [commandCenternextTrackCommand];
[nextCommandsetEnabled:YES];
[nextCommandaddTarget:selfaction:@selector(remoteNextEvent)];
MPRemoteCommand*previousCommand = [commandCenterpreviousTrackCommand];
[previousCommandsetEnabled:YES];
[previousCommandaddTarget:selfaction:@selector(remotePreviousEvent)];
if(@available(iOS9.1, *)) {
MPRemoteCommand*changePlaybackPositionCommand = [commandCenterchangePlaybackPositionCommand];
[changePlaybackPositionCommandsetEnabled:YES];
[changePlaybackPositionCommandaddTarget:selfaction:@selector(remoteChangePlaybackPosition:)];
}
}
//暂停
-(void)remotePauseEvent {
[musicManager pause];
[self updateLockScreenInfo];
}
//播放
-(void) remotePlayEvent{
[musicManager play];
[self updateLockScreenInfo];
}
//快进
-(void) remoteNextEvent{
[musicManager fastForward];
[self updateLockScreenInfo];
}
//快退
-(void)remotePreviousEvent{
[musicManager fastBack];
[self updateLockScreenInfo];
}
//跳跃
-(void)remoteChangePlaybackPosition:(MPRemoteCommand*)command{
//跳跃操作
[self updateLockScreenInfo];
}
- (void)dealloc {
[self removeCommandCenterTargets];
}
- (void)removeCommandCenterTargets {
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[[commandCenterplayCommand]removeTarget:self];
[[commandCenterpauseCommand]removeTarget:self];
[[commandCenternextTrackCommand] removeTarget:self];
[[commandCenterpreviousTrackCommand] removeTarget:self];
if(@available(iOS9.1, *)) {
[commandCenter.changePlaybackPositionCommand removeTarget:self];
}
}