PlayerManger.m
10.3 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// PlayerManger.m
// DreamSleep
//
// Created by peter on 2022/10/24.
//
#import "PlayerManger.h"
#import <FSAudioController.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "SubAudioModel.h"
@interface PlayerManger ()
/// AudioStream 播放器
@property (nonatomic, strong) FSAudioStream *audioStream;
/// 播放进度定时器
@property (nonatomic, strong) CADisplayLink *displayLink;
/// 是否正在拖动滑块
@property (nonatomic, assign) BOOL isDraging;
/// streamState
@property (nonatomic, assign) FSAudioStreamState streamState;
/// 播放数据model
@property (nonatomic, strong) SubAudioModel *playItem;
/// 控制中心信息
@property (nonatomic, strong) NSMutableDictionary *remoteInfoDictionary;
/// 是否进入后台
@property (nonatomic, assign) BOOL isBackground;
@end
@implementation PlayerManger
- (instancetype)initWithItem:(id)item {
if (self = [super init]) {
self.playItem = (SubAudioModel *)item;
[self addStreamStateObserver];
[self addPlayerObserver];
[self addRemoteControlHandler];
}
return self;
}
- (void)dealloc {
[self.displayLink invalidate];
self.displayLink = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter];
[[center playCommand] removeTarget:self];
[[center pauseCommand] removeTarget:self];
[[center nextTrackCommand] removeTarget:self];
[[center previousTrackCommand] removeTarget:self];
[center.changePlaybackPositionCommand removeTarget:self];
}
#pragma mark - public
- (void)startPlay {
[self.audioStream playFromURL:[NSURL URLWithString:self.playItem.audio_url]];
// 设置音频锁屏界面信息
[self _addPlayingCenterInfo];
}
- (void)seekToPosition:(float)position {
FSStreamPosition pos = {0};
pos.position = position;
[self.audioStream seekToPosition:pos];
}
- (void)stop {
if (self.streamState == kFsAudioStreamPlaying) {
[self.audioStream pause];
}
[self.audioStream stop];
}
- (void)playOrPause {
// 播放
if (self.streamState == kFsAudioStreamStopped || self.streamState == kFsAudioStreamRetrievingURL) {
[self.audioStream play];
} else if (self.streamState == kFsAudioStreamPlaying) {
// 暂停播放
[self.audioStream pause];
} else if (self.streamState == kFsAudioStreamPaused) {
// 恢复播放
[self.audioStream pause];
}
}
- (void)updatePlayingCenterInfo {
if (!self.isBackground) { return; }
self.remoteInfoDictionary[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:self.audioStream.currentTimePlayed.playbackTimeInSeconds];
self.remoteInfoDictionary[MPMediaItemPropertyPlaybackDuration] = [NSNumber numberWithDouble:self.audioStream.duration.playbackTimeInSeconds];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = self.remoteInfoDictionary;
}
#pragma mark - 播放状态监控
- (void)addStreamStateObserver {
WS(weakSelf);
[self.audioStream setOnStateChange:^(FSAudioStreamState state) {
weakSelf.streamState = state;
if (weakSelf.playStateChange) {
weakSelf.playStateChange(state == kFsAudioStreamPlaying);
}
[weakSelf.displayLink setPaused:!(state == kFsAudioStreamPlaying)];
switch (state) {
case kFsAudioStreamRetrievingURL:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
DSLog(@"retrieving URL -- 检索文件");
break;
case kFsAudioStreamStopped:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
DSLog(@"kFsAudioStreamStopped --- 停止播放了");
break;
case kFsAudioStreamBuffering: {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
DSLog(@"buffering --- 缓存中");
break;
}
case kFsAudioStreamPaused:
DSLog(@"暂停了");
break;
case kFsAudioStreamSeeking:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
DSLog(@"kFsAudioStreamSeeking -- 快进 或者 快退");
break;
case kFsAudioStreamPlaying:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
DSLog(@"播放ing...");
break;
case kFsAudioStreamFailed:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
DSLog(@"音频文件加载失败");
break;
case kFsAudioStreamPlaybackCompleted:
// 单曲循环
[weakSelf.audioStream play];
break;
case kFsAudioStreamRetryingStarted:
DSLog(@"回放失败");
break;
case kFsAudioStreamRetryingSucceeded:
DSLog(@"重试成功");
break;
case kFsAudioStreamRetryingFailed:
DSLog(@"Failed to retry playback -- 重试失败");
break;
default:
break;
}
}];
self.audioStream.onFailure = ^(FSAudioStreamError error, NSString *errorDescription) {
DSLog(@"音频加载失败:%ld", error);
};
}
#pragma mark - noti
- (void)addPlayerObserver {
// 将要进入后台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
// 已经进入前台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
// 监听播放器被打断(别的软件播放音乐,来电话)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerAudioBeInterrupted:)
name:AVAudioSessionInterruptionNotification
object:[AVAudioSession sharedInstance]];
}
- (void)playerWillResignActive {
self.isBackground = YES;
}
- (void)playerDidEnterForeground {
self.isBackground = NO;
}
- (void)playerAudioBeInterrupted:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
if ([userInfo[AVAudioSessionInterruptionTypeKey] integerValue] == 1) { // 打断开始
[self _pause];
} else { // 打断结束
if ([userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue] == 1) {
[self _play];
}
}
}
#pragma mark - 息屏播放
- (void)addRemoteControlHandler {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter];
[self _addRemoteCommand:center.playCommand selector:@selector(_play)];
[self _addRemoteCommand:center.pauseCommand selector:@selector(_pause)];
}
#pragma mark - private
- (void)display {
FSStreamPosition curPosition = self.audioStream.currentTimePlayed;
FSStreamPosition endPosition = self.audioStream.duration;
float progress = curPosition.position;
NSString *curTime = [NSString stringWithFormat:@"%02i:%02i", curPosition.minute, curPosition.second];
NSString *totalTime = [NSString stringWithFormat:@"%02i:%02i", endPosition.minute, endPosition.second];
if (self.progressChange) {
self.progressChange(progress, curTime, totalTime);
}
}
- (void)_addPlayingCenterInfo {
self.remoteInfoDictionary = [NSMutableDictionary dictionary];
self.remoteInfoDictionary[MPMediaItemPropertyTitle] = self.playItem.audio_name;
self.remoteInfoDictionary[MPMediaItemPropertyAlbumTitle] = @"小梦睡眠";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.playItem.audio_pic]];
UIImage *artworkImg = [UIImage imageWithData:imageData];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithBoundsSize:artworkImg.size requestHandler:^UIImage * _Nonnull(CGSize size) {
return artworkImg;
}];
self.remoteInfoDictionary[MPMediaItemPropertyArtwork] = artwork;
self.remoteInfoDictionary[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithFloat:1.0];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = self.remoteInfoDictionary;
}
- (void)_play {
if (self.streamState == kFsAudioStreamStopped || self.streamState == kFsAudioStreamRetrievingURL) {
[self.audioStream play];
} else if (self.streamState == kFsAudioStreamPaused) {
// 恢复播放
[self.audioStream pause];
}
}
- (void)_pause {
if (self.streamState == kFsAudioStreamPlaying) {
// 暂停播放
[self.audioStream pause];
}
}
- (void)_addRemoteCommand:(MPRemoteCommand *)command selector:(SEL)selector {
WS(weaksSelf);
[command addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
if ([weaksSelf respondsToSelector:selector]) {
IMP imp = [weaksSelf methodForSelector:selector];
void (*func)(id, SEL) = (void *)imp;
func(weaksSelf, selector);
}
return MPRemoteCommandHandlerStatusSuccess;
}];
}
#pragma mark - lazy
- (FSAudioStream *)audioStream {
if (!_audioStream) {
_audioStream = [[FSAudioStream alloc] init];
_audioStream.strictContentTypeChecking = NO;
_audioStream.defaultContentType = @"audio/mpeg";
_audioStream.volume = .5;
[_audioStream setPlayRate:1.0];
}
return _audioStream;
}
- (CADisplayLink *)displayLink {
if (!_displayLink) {
TimerProxy *proxy = [TimerProxy proxyWithTarget:self];
_displayLink = [CADisplayLink displayLinkWithTarget:proxy selector:@selector(display)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
return _displayLink;
}
@end