Skip to content

Commit 0d8eec0

Browse files
sumengsumeng
authored andcommitted
AudioPlayView支持先下载再播放
1 parent 8ff1073 commit 0d8eec0

5 files changed

Lines changed: 117 additions & 38 deletions

File tree

Coding_iOS/Util/Audio/AudioAmrUtil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
+ (NSString *)convertedAmrFromWave:(NSString *)waveFile;
2121
+ (NSString *)convertedWaveFromAmr:(NSString *)amrFile;
22-
+ (BOOL)cleanup;
22+
+ (BOOL)cleanCache;
2323

2424
@end

Coding_iOS/Util/Audio/AudioAmrUtil.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ + (NSString *)encodeWaveToAmr:(NSString *)waveFile
2626
if ([[NSFileManager defaultManager] fileExistsAtPath:amrFile]) {
2727
[[NSFileManager defaultManager] removeItemAtPath:amrFile error:nil];
2828
}
29-
NSTimeInterval date1 = [[NSDate date] timeIntervalSince1970];
3029
NSData *armData = EncodeWAVEToAMR([NSData dataWithContentsOfFile:waveFile], nChannels, nBitsPerSample);
3130
[armData writeToFile:amrFile atomically:YES];
32-
NSTimeInterval date2 = [[NSDate date] timeIntervalSince1970];
33-
NSLog(@"encodeWaveToAmr cost:%fs", date2-date1);
3431
return amrFile;
3532
}
3633

@@ -71,7 +68,7 @@ + (NSString *)convertDir {
7168
return dir;
7269
}
7370

74-
+ (BOOL)cleanup {
71+
+ (BOOL)cleanCache {
7572
return [[NSFileManager defaultManager] removeItemAtPath:[self convertDir] error:nil];
7673
}
7774

Coding_iOS/Views/AudioView/AudioPlayView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
- (void)play;
2020
- (void)stop;
2121

22+
+ (BOOL)cleanCache;
23+
2224
- (void)didDownloadStarted;
2325
- (void)didDownloadFinished;
2426
- (void)didDownloadError:(NSError *)error;

Coding_iOS/Views/AudioView/AudioPlayView.m

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "AudioPlayView.h"
1010
#import "AudioManager.h"
1111
#import "AudioAmrUtil.h"
12+
#import "Coding_FileManager.h"
1213

1314
@interface AudioPlayView ()
1415

@@ -41,29 +42,56 @@ - (void)initAudioPlayView {
4142
[self addTarget:self action:@selector(onClicked:) forControlEvents:UIControlEventTouchUpInside];
4243
}
4344

45+
- (id)validator {
46+
if (_validator) {
47+
return _validator;
48+
}
49+
else {
50+
return _url;
51+
}
52+
}
53+
4454
- (void)play {
4555
[self stop];
4656

4757
if (_url == nil) {
4858
return;
4959
}
50-
NSString *file = nil;
5160
if ([_url isFileURL]) {
52-
NSString *audioFile = _url.path;
53-
if ([@"amr" isEqualToString:audioFile.pathExtension]) {
54-
file = [AudioAmrUtil convertedWaveFromAmr:audioFile];
55-
if (file == nil) {
56-
file = [AudioAmrUtil decodeAmrToWave:audioFile];
57-
}
61+
[self play:_url.path];
62+
}
63+
else {
64+
NSString *file = [[self class] downloadFile:_url.absoluteString];
65+
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
66+
[self play:file];
5867
}
5968
else {
60-
file = audioFile;
69+
[AudioManager shared].validator = self.validator;
70+
[self startDownload:_url];
6171
}
6272
}
73+
}
74+
75+
- (void)play:(NSString *)file {
76+
[self stop];
77+
if (file.length == 0) {
78+
return;
79+
}
80+
81+
NSString *f = nil;
82+
if ([@"amr" isEqualToString:file.pathExtension]) {
83+
f = [AudioAmrUtil convertedWaveFromAmr:file];
84+
if (f == nil) {
85+
f = [AudioAmrUtil decodeAmrToWave:file];
86+
}
87+
}
88+
else {
89+
f = file;
90+
}
6391

6492
_isPlaying = YES;
6593
[AudioManager shared].delegate = self;
66-
[[AudioManager shared] play:file validator:_validator];
94+
[[AudioManager shared] play:f validator:self.validator];
6795
}
6896

6997
- (void)stop {
@@ -77,6 +105,29 @@ - (void)onClicked:(id)sender {
77105

78106
#pragma mark - Download
79107

108+
- (void)startDownload:(NSURL *)url {
109+
if (url == nil) {
110+
return;
111+
}
112+
NSURLRequest *request = [NSURLRequest requestWithURL:url];
113+
NSProgress *progress;
114+
NSURLSessionDownloadTask *downloadTask = [[Coding_FileManager af_manager] downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
115+
return [NSURL fileURLWithPath:[[self class] downloadFile:response.URL.absoluteString]];
116+
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
117+
if (error) {
118+
[self didDownloadError:error];
119+
}
120+
else {
121+
if ([self.validator isEqual:[AudioManager shared].validator]) {
122+
[self play:filePath.path];
123+
}
124+
[self didDownloadFinished];
125+
}
126+
}];
127+
[downloadTask resume];
128+
[self didDownloadStarted];
129+
}
130+
80131
- (void)didDownloadStarted {
81132

82133
}
@@ -89,6 +140,24 @@ - (void)didDownloadError:(NSError *)error {
89140

90141
}
91142

143+
#pragma mark - FileManager
144+
145+
+ (NSString *)downloadDir {
146+
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
147+
NSString *dir = [docDir stringByAppendingPathComponent:@"AudioDownload"];
148+
[[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:NO attributes:nil error:nil];
149+
return dir;
150+
}
151+
152+
+ (NSString *)downloadFile:(NSString *)url {
153+
NSString *file = [[url md5Str] stringByAppendingPathExtension:[url pathExtension]];
154+
return [[self downloadDir] stringByAppendingPathComponent:file];
155+
}
156+
157+
+ (BOOL)cleanCache {
158+
return [[NSFileManager defaultManager] removeItemAtPath:[self downloadDir] error:nil];
159+
}
160+
92161
#pragma mark - AudioManagerDelegate
93162

94163
- (void)didAudioPlayStarted:(AudioManager *)am {

Coding_iOS/Views/AudioView/BubblePlayView.m

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ @interface BubblePlayView ()
1313
@property (nonatomic, strong) UIImageView *bgImageView;
1414
@property (nonatomic, strong) UIImageView *playImageView;
1515
@property (nonatomic, strong) UILabel *durationLbl;
16+
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
1617

1718
@end
1819

@@ -33,6 +34,12 @@ - (instancetype)initWithFrame:(CGRect)frame {
3334
_durationLbl.textColor = [UIColor blackColor];
3435
[self addSubview:_durationLbl];
3536

37+
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
38+
_activityView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
39+
_activityView.hidesWhenStopped = YES;
40+
[_activityView stopAnimating];
41+
[self addSubview:_activityView];
42+
3643
self.showBgImg = YES;
3744
self.type = BubbleTypeLeft;
3845
self.duration = 0;
@@ -90,53 +97,57 @@ - (void)setShowBgImg:(BOOL)showBgImg {
9097
_bgImageView.hidden = !showBgImg;
9198
}
9299

100+
#pragma mark - Animation
101+
102+
- (void)startPlayingAnimation {
103+
if (_type == BubbleTypeRight) {
104+
_playImageView.image = [UIImage animatedImageWithImages:@[[UIImage imageNamed:@"btn_file_cancel"], [UIImage imageNamed:@"btn_file_reDo"], [UIImage imageNamed:@"button_download_cancel"]] duration:0.8];
105+
}
106+
else {
107+
_playImageView.image = [UIImage animatedImageWithImages:@[[UIImage imageNamed:@"btn_file_cancel"], [UIImage imageNamed:@"btn_file_reDo"], [UIImage imageNamed:@"button_download_cancel"]] duration:0.8];
108+
}
109+
[_playImageView startAnimating];
110+
}
111+
112+
- (void)stopPlayingAnimation {
113+
[_playImageView stopAnimating];
114+
if (_type == BubbleTypeRight) {
115+
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
116+
}
117+
else {
118+
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
119+
}
120+
}
121+
93122
#pragma mark - Download
94123

95124
- (void)didDownloadStarted {
96-
125+
[_activityView startAnimating];
97126
}
98127

99128
- (void)didDownloadFinished {
100-
129+
[_activityView stopAnimating];
101130
}
102131

103132
- (void)didDownloadError:(NSError *)error {
104-
133+
[_activityView stopAnimating];
105134
}
106135

107136
#pragma mark - AudioManagerDelegate
108137

109138
- (void)didAudioPlayStarted:(AudioManager *)am {
110139
[super didAudioPlayStarted:am];
111-
// if (_type == BubbleTypeRight) {
112-
// _playImageView.image = [UIImage animatedImageNamed:@"bubble_right_playing_" duration:1];
113-
// }
114-
// else {
115-
// _playImageView.image = [UIImage animatedImageNamed:@"bubble_left_playing_" duration:1];
116-
// }
117-
// [_playImageView startAnimating];
140+
[self startPlayingAnimation];
118141
}
119142

120143
- (void)didAudioPlayStoped:(AudioManager *)am successfully:(BOOL)successfully {
121144
[super didAudioPlayStoped:am successfully:successfully];
122-
[_playImageView stopAnimating];
123-
if (_type == BubbleTypeRight) {
124-
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
125-
}
126-
else {
127-
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
128-
}
145+
[self stopPlayingAnimation];
129146
}
130147

131148
- (void)didAudioPlay:(AudioManager *)am err:(NSError *)err {
132149
[super didAudioPlay:am err:err];
133-
[_playImageView stopAnimating];
134-
if (_type == BubbleTypeRight) {
135-
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
136-
}
137-
else {
138-
_playImageView.image = [UIImage imageNamed:@"add_user_icon"];
139-
}
150+
[self stopPlayingAnimation];
140151
}
141152

142153
@end

0 commit comments

Comments
 (0)