JGMusicManager.m
5.54 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
//
// JGMusicManager.m
// 视频音频-Demo
//
// Created by stkcctv on 16/8/25.
// Copyright © 2016年 stkcctv. All rights reserved.
//
#import "JGMusicManager.h"
@implementation JGMusicManager
+ (instancetype)shareMusicManager {
static JGMusicManager *musicManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
musicManager = [[JGMusicManager alloc] init];
[musicManager remoteControlEventHandler];
});
return musicManager;
}
- (instancetype)init {
if (self = [super init]) {
_player = [[AVPlayer alloc] init];
}
return self;
}
- (void)playStop {
[self playerPause];
}
//播放或暂停
- (void)playAndPause {
if (self.isPlay) {
[self playerPause];
} else {
[self playerPlay];
}
}
//播放
- (void)playerPlay {
[_player play];
_isPlay = YES;
}
//暂停
- (void)playerPause {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pauseVocieNotification" object:nil];
[_player pause];
_isPlay = NO;
}
//播放上一首
- (void)playPrevious {
if (self.index == 0) {
self.index = self.musicArray.count - 1;
}else {
self.index--;
}
}
//播放下一首
- (void)playNext {
if (self.index == self.musicArray.count - 1) {
self.index = 0;
}else {
self.index++;
}
}
- (void)playerVolumeWithVolumeFloat:(CGFloat)volumeFloat {
self.player.volume = volumeFloat;
}
- (void)playerProgressWithProgressFloat:(CGFloat)progressFloat {
[self.player seekToTime:CMTimeMakeWithSeconds(progressFloat, 1) completionHandler:^(BOOL finished) {
[self playerPlay];
}];
}
- (void)replaceItemWithDictionary:(NSDictionary *)dictionary {
[self setLockingInfo:dictionary];
NSString *urlString = dictionary[@"videoUrl"];
urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:urlString]];
[self playerPause];
[self.player replaceCurrentItemWithPlayerItem:item];
[self playerPlay];
[AlertViewTool hideHUDView];
}
//设置锁屏信息
- (void)setLockingInfo:(NSDictionary *)dictionary
{
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
//数据信息
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
// //图片
// MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageWithUrlString:model.icon]];
// [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
//
// //当前播放时间
// [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] currentPlaybackTime]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
// //速率
// [songInfo setObject:[NSNumber numberWithFloat:1.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate];
// //剩余时长
// [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] duration]] forKey:MPMediaItemPropertyPlaybackDuration];
//设置标题
[songInfo setObject:dictionary[@"cmsColumnName"] ? dictionary[@"cmsColumnName"] : @"" forKey:MPMediaItemPropertyTitle];
//设置副标题
[songInfo setObject:dictionary[@"title"] ? dictionary[@"title"] : @"" forKey:MPMediaItemPropertyArtist];
//设置音频数据信息
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
}
#pragma mark - private method
// 在需要处理远程控制事件的具体控制器或其它类中实现
- (void)remoteControlEventHandler {
// 直接使用sharedCommandCenter来获取MPRemoteCommandCenter的shared实例
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
// 播放, 暂停, 上下曲的命令默认都是启用状态, 即enabled默认为YES
// 播放
@try {
[commandCenter.playCommand addTarget:self action:@selector(playAction:)];//playAction:)
// 暂停
[commandCenter.pauseCommand addTarget:self action:@selector(pauseAction:)];
// 上一曲
[commandCenter.previousTrackCommand addTarget:self action:@selector(previousTrackAction:)];
// 下一曲
[commandCenter.nextTrackCommand addTarget:self action:@selector(nextTrackAction:)];
// 启用耳机的播放/暂停命令 (耳机上的播放按钮触发的命令)
commandCenter.togglePlayPauseCommand.enabled = YES;
// 为耳机的按钮操作添加相关的响应事件
[commandCenter.togglePlayPauseCommand addTarget:self action:@selector(playOrPauseAction:)];
}
@catch (NSException *exception) {
NSLog(@"exception = %@", exception);
}
@finally {
}
}
- (void)playAction:(MPRemoteCommandCenter *)sender {
[self playerPlay];
}
- (void)pauseAction:(MPRemoteCommandCenter *)sender {
[self playerPause];
}
- (void)previousTrackAction:(MPRemoteCommandCenter *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(lastMusic)]) {
[self.delegate lastMusic];
}
}
- (void)nextTrackAction:(MPRemoteCommandCenter *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(NextMusic)]) {
[self.delegate NextMusic];
}
}
- (void)playOrPauseAction:(MPRemoteCommandCenter *)sender {
[self playAndPause];
}
@end