标签:
1、首先要音乐支持后台播放
(1)在info.plist中添加
Required background modes
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
(3)在后台注册事件:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
2、锁屏设置
判断锁屏事件,进行事件处理
-(void)remoteControlReceivedWithEvent:(UIEvent *)event { switch (event.subtype) { case UIEventSubtypeRemoteControlPause: [self.currentAudioPlayer pause]; break; case UIEventSubtypeRemoteControlNextTrack: if (self.rotateCell) { [self.rotateCell stopRotate]; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; SWTMusic *music = self.musicArray[indexPath.item]; [SWTMusicTool stopMusic:music.filename]; int index = indexPath.item + 1; if (index >= self.musicArray.count) { index = 0; } NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:index inSection:0]; [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; SWTMusicCell *cell = [self.tableView cellForRowAtIndexPath:newIndexPath]; self.rotateCell = cell; [cell startRotate]; SWTMusic *nextMusic = self.musicArray[index]; [self showInfoInLockedScreen:nextMusic]; self.currentAudioPlayer = [SWTMusicTool playMusic:nextMusic.filename]; self.currentAudioPlayer.delegate = self; } break; case UIEventSubtypeRemoteControlPreviousTrack: if (self.rotateCell) { [self.rotateCell stopRotate]; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; int index = indexPath.item - 1; if (index < 0) { index = self.musicArray.count-1; } NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:index inSection:0]; SWTMusic *music = self.musicArray[indexPath.item]; [SWTMusicTool stopMusic:music.filename]; [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; SWTMusicCell *cell = [self.tableView cellForRowAtIndexPath:newIndexPath]; self.rotateCell = cell; [cell startRotate]; SWTMusic *nextMusic = self.musicArray[index]; [self showInfoInLockedScreen:nextMusic]; self.currentAudioPlayer = [SWTMusicTool playMusic:nextMusic.filename]; self.currentAudioPlayer.delegate = self; } break; case UIEventSubtypeRemoteControlPlay: [self.currentAudioPlayer play]; break; default: break; } }
4、锁屏信息
-(void)showInfoInLockedScreen:(SWTMusic*)music { // 这种方式,不能将信息正确显示在锁屏上 // NSMutableDictionary *info = [NSMutableDictionary dictionary]; // info[MPMediaItemPropertyTitle] = music.name; // info[MPMediaItemPropertyArtist] = music.singer; // info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:music.icon]]; // // [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info]; NSMutableDictionary *songInfo = [ [NSMutableDictionary alloc] init]; MPMediaItemArtwork *albumArt = [ [MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:music.icon]]; //设置播放进度条 double currentTime = self.currentAudioPlayer.currentTime; double duration = self.currentAudioPlayer.duration ; [ songInfo setObject:music.name forKey:MPMediaItemPropertyTitle ]; [ songInfo setObject:music.singer forKey:MPMediaItemPropertyArtist ]; [ songInfo setObject:music.singer forKey:MPMediaItemPropertyAlbumTitle ]; [ songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork ]; [songInfo setObject:@(currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [songInfo setObject:@(duration) forKey:MPMediaItemPropertyPlaybackDuration]; [ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ]; }
标签:
原文地址:http://www.cnblogs.com/aminopeptidase/p/4817954.html