AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
}</pre>
<pre></pre>
<pre name="code"class="objc">//重寫父類方法,接受外部事件的處理
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
NSLog(@"remote");
if(receivedEvent.type == UIEventTypeRemoteControl) {
switch(receivedEvent.subtype) {// 得到事件類型
caseUIEventSubtypeRemoteControlTogglePlayPause:// 暫停 ios6
[_player musicPause];// 調用你所在項目的暫停按鈕的響應方法 下面的也是如此
break;
caseUIEventSubtypeRemoteControlPreviousTrack:// 上一首
[self lastMusic];
break;
caseUIEventSubtypeRemoteControlNextTrack:// 下一首
[self nextMusic];
break;
caseUIEventSubtypeRemoteControlPlay://播放
[_player musicPlay];
break;
caseUIEventSubtypeRemoteControlPause:// 暫停 ios7
[_player musicPause];
break;
default:
break;
}
}
}</pre><pre name="code"class="objc"><pre name="code"class="objc">- (void)configNowPlayingCenter {
NSLog(@"鎖屏設置");
// BASE_INFO_FUN(@"配置NowPlayingCenter");
YBVideoListModel * list = _model.audioList[_currentIndexPath.row];</pre><pre name="code"class="objc"><span style="white-space:pre"> </span>//以下代碼是加載鎖屏顯示網絡圖片以及其他設置
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:list.thumbUrl] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if(image == nil) {
image = [UIImage imageNamed:@"default_music"];
}
NSMutableDictionary * info = [NSMutableDictionary dictionary];
//音樂的標題
[info setObject:list.title forKey:MPMediaItemPropertyTitle];
//音樂的藝術家
NSString *author= list.singer;
[info setObject:author forKey:MPMediaItemPropertyArtist];
//音樂的播放時間
[info setObject:@(_player.player.currentTime.value) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//音樂的播放速度
[info setObject:@(1) forKey:MPNowPlayingInfoPropertyPlaybackRate];
//音樂的總時間
[info setObject:@(list.duration) forKey:MPMediaItemPropertyPlaybackDuration];
//音樂的封面
MPMediaItemArtwork * artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[info setObject:artwork forKey:MPMediaItemPropertyArtwork];
//完成設置
[[MPNowPlayingInfoCenter defaultCenter]setNowPlayingInfo:info];
}];
}</pre><br>
<br>
<pre></pre>
<br>
</pre>