本文实例为大家分享了iOS锁屏页面控制音乐播放的具体代码,供大家参考,具体内容如下
?
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
//1、调整音频会话设置,确保应用进入后台或静音开关已开启时音频仍将继续播放
//2、锁屏状态下显示媒体信息
//3、锁屏上的空间可以控制音频播放
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) UIButton *playButton;
@end
@implementation ViewController
- ( void )viewDidLoad {
[super viewDidLoad];
UIButton * playButton = [UIButton buttonWithType:UIButtonTypeSystem];
playButton.frame = CGRectMake(0, 0, 200, 40);
playButton.center = self.view.center;
[playButton setTitle:@ "在后台播放音频" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playMusicInBackground:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
NSError *playerInitError = nil;
NSString *audioPath =
[[NSBundle mainBundle] pathForResource:@ "background_audio"
ofType:@ "mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioURL
error:&playerInitError];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *activeError = nil;
if (![session setActive:YES error:&activeError]) {
NSLog(@ "Failed to set active audio session!" );
}
//No.1
//开始写代码,调整音频会话设置,确保即便应用进入后台或静音开关已开启,音频仍将继续播放
NSError *categoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&categoryError];
//end_code
}
- ( void )playMusicInBackground:(id)sender {
if ([self.audioPlayer isPlaying]) {
[self.audioPlayer stop];
[self.playButton setTitle:@ "正在播放音乐"
forState:UIControlStateNormal];
} else {
UIImage *lockImage = [UIImage imageNamed:@ "belongToMe.jpg" ];
MPMediaItemArtwork *artwork =
[[MPMediaItemArtwork alloc] initWithImage:lockImage];
NSDictionary *mediaDict =
@{
MPMediaItemPropertyTitle: @ "BackgroundTask Audio" ,
MPMediaItemPropertyMediaType: @(MPMediaTypeAnyAudio),
MPMediaItemPropertyPlaybackDuration:
@(self.audioPlayer.duration),
MPNowPlayingInfoPropertyPlaybackRate: @1.0,
MPNowPlayingInfoPropertyElapsedPlaybackTime:
@(self.audioPlayer.currentTime),
MPMediaItemPropertyAlbumArtist: @ "Some User" ,
MPMediaItemPropertyArtist: @ "Some User" ,
MPMediaItemPropertyArtwork: artwork };
[self.audioPlayer play];
[self.playButton setTitle:@ "停止播放后台音乐"
forState:UIControlStateNormal];
//No.2
//开始写代码,将媒体信息显示在锁定屏幕上,并使锁屏上控件可以控制音频播放
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//end_code
}
}
//No.3
//开始写代码,响应远程控制,使得进入锁屏状态后可以控制音乐“播放”和“暂停”
- ( void ) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.audioPlayer play];
break ;
case UIEventSubtypeRemoteControlPause:
[self.audioPlayer pause];
break ;
default :
NSLog(@ "没有处理过这个事件------receivedEvent.subtype==%ld" ,( long )receivedEvent.subtype);
break ;
}
}
}
//end_code
- ( void )didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 36
-
2025-05-25 39
-
2025-05-25 58
-
2025-05-25 102
-
2025-05-29 94
热门评论