廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<pre name="code" class="objc">appDelegate裡面加入如下代碼獲取後台播放權限</pre><pre name="code" class="objc">- (void)setAudioBackstagePlay{ 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) { // 得到事件類型 case UIEventSubtypeRemoteControlTogglePlayPause: // 暫停 ios6 [_player musicPause]; // 調用你所在項目的暫停按鈕的響應方法 下面的也是如此 break; case UIEventSubtypeRemoteControlPreviousTrack: // 上一首 [self lastMusic]; break; case UIEventSubtypeRemoteControlNextTrack: // 下一首 [self nextMusic]; break; case UIEventSubtypeRemoteControlPlay: //播放 [_player musicPlay]; break; case UIEventSubtypeRemoteControlPause: // 暫停 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>
以上所述是小編給大家介紹的 iOS中關於音樂鎖屏控制音樂(鎖屏信息設置)的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!