iOS中關于音樂鎖屏控制音樂(鎖屏信息設置)的實例代碼
更新時間:2017年01月20日 16:51:54 作者:想起那天夕陽下的奔跑
這篇文章主要介紹了 iOS中關于音樂鎖屏控制音樂(鎖屏信息設置)的實例代碼,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<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中關于音樂鎖屏控制音樂(鎖屏信息設置)的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
詳解iOS App中UISwitch開關組件的基本創(chuàng)建及使用方法
UISwitch組件就是我們平時在iOS設置菜單中開到的那種左右滑動的開關按鈕,當然我們在開發(fā)時可以進行更多的自定義,這里我們就來詳解iOS App中UISwitch開關組件的基本創(chuàng)建及使用方法2016-05-05iOS開發(fā)中UITabBarController的使用示例
這篇文章主要介紹了iOS開發(fā)中UITabBarController的使用示例,代碼基于Objective-C進行演示,需要的朋友可以參考下2015-09-09IOS計步器功能實現之Healthkit和CMPedometer
現在越來越多的人關注運動和健康,iOS系統也在很早的時候就自帶了健康APP,下面詳細描述一下在我們開發(fā)中,怎么實現計步器功能。2016-08-08使用iOS控件UICollectionView生成可拖動的桌面的實例
本篇文章主要介紹了使用iOS控件UICollectionView生成可拖動的桌面,具有一定的參考價值,有需要的可以了解一下。2016-12-12IOS開發(fā)之多線程NSThiread GCD NSOperation Runloop
這篇文章主要介紹了IOS多線程開發(fā),主要用到NSThiread、GCD、 NSOperation、Runloop,有詳細的原理解析和實例代碼,對多線程感興趣的同學,可以參考下2021-04-04