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
//
// IBTAVAudioPlayWrapper.m
// XFFruit
//
// Created by Xummer on 4/17/15.
// Copyright (c) 2015 Xummer. All rights reserved.
//
#import "IBTAVAudioPlayWrapper.h"
#import "IBTAudioController.h"
@import AVFoundation;
@interface IBTAVAudioPlayWrapper ()
<
AVAudioPlayerDelegate
>
{
AVAudioPlayer* m_audioPlayer;
}
@property (copy, nonatomic) AduioPlayCompleteBlock m_completeBlock;
@end
@implementation IBTAVAudioPlayWrapper
#pragma mark - Life Cycle
- (id)init {
self = [super init];
if (!self) {
return nil;
}
return self;
}
- (void)dealloc {
m_audioPlayer = nil;
}
#pragma mark - Public Method
- (void)playWithPath:(NSString *)path {
// Can not be remote URL
NSURL *audioUrl = [NSURL fileURLWithPath:path];
[self playWithUrl:audioUrl];
}
- (void)playWithUrl:(NSURL *)audioUrl {
if (m_audioPlayer) {
m_audioPlayer.delegate = nil;
[m_audioPlayer stop];
}
NSError *error = nil;
m_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioUrl
error:&error];
if (error) {
CLog(@"%@", error);
return;
}
[self addProximityObersever];
m_audioPlayer.delegate = self;
m_audioPlayer.volume = 1.0f;
[m_audioPlayer prepareToPlay];
[m_audioPlayer play];
}
- (void)playWithData:(NSData *)data {
if (m_audioPlayer) {
m_audioPlayer.delegate = nil;
[m_audioPlayer stop];
}
NSError *error = nil;
m_audioPlayer = [[AVAudioPlayer alloc] initWithData:data
error:&error];
if (error) {
CLog(@"%@", error);
return;
}
[self addProximityObersever];
m_audioPlayer.delegate = self;
m_audioPlayer.volume = 1.0f;
[m_audioPlayer prepareToPlay];
[m_audioPlayer play];
}
- (void)playWithPath:(NSString *)path complete:(AduioPlayCompleteBlock)complete {
self.m_completeBlock = complete;
[self playWithPath:path];
}
- (void)playWithUrl:(NSURL *)audioUrl complete:(AduioPlayCompleteBlock)complete {
self.m_completeBlock = complete;
[self playWithUrl:audioUrl];
}
- (void)playWithData:(NSData *)data complete:(AduioPlayCompleteBlock)complete {
self.m_completeBlock = complete;
[self playWithData:data];
}
- (void)stopPlay {
if ([m_audioPlayer isPlaying]) {
[m_audioPlayer stop];
[[IBTAudioController sharedController] deactivate];
}
}
#pragma mark - Private Method
#pragma mark -
- (void)addProximityObersever {
[[IBTAudioController sharedController] switchToPlaybackMode];
[[IBTAudioController sharedController] activate];
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(proximitySensorStateChange:)
name:UIDeviceProximityStateDidChangeNotification
object:nil];
}
- (void)removeProximityObersever {
// 关闭距离传感器
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceProximityStateDidChangeNotification
object:nil];
[[IBTAudioController sharedController] switchToPlaybackMode];
}
- (void)proximitySensorStateChange:(NSNotificationCenter *)obj {
if ([[UIDevice currentDevice] proximityState]) {
[[IBTAudioController sharedController] switchToPlayAndRecordMode];
}
else {
[[IBTAudioController sharedController] switchToPlaybackMode];
}
}
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self removeProximityObersever];
[m_audioPlayer stop];
m_audioPlayer = nil;
if (_m_completeBlock) {
_m_completeBlock(YES, nil);
self.m_completeBlock = nil;
[[IBTAudioController sharedController] deactivate];
}
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
[self removeProximityObersever];
if (_m_completeBlock) {
_m_completeBlock(NO, error);
self.m_completeBlock = nil;
[[IBTAudioController sharedController] deactivate];
}
}
/* audioPlayerBeginInterruption: is called when the audio session has been interrupted while the player was playing. The player will have been paused. */
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
}
/* audioPlayerEndInterruption:withOptions: is called when the audio session interruption has ended and this player had been interrupted while playing. */
/* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
// switch (flags) {
// case AVAudioSessionInterruptionFlags_ShouldResume:
//
// break;
//
// default:
// break;
// }
}
@end