废话不多说了,直接给大家贴代码了,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<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 { // 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中关于音乐锁屏控制音乐(锁屏信息设置)的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/baidu_36500903/article/details/54631955