-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVAudioPlayer+AVAudioPlayer_Fading.m
74 lines (57 loc) · 1.72 KB
/
AVAudioPlayer+AVAudioPlayer_Fading.m
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
//
// AVAudioPlayer+AVAudioPlayer_Fading.m
// Tipi
//
// Created by Leo on 04/06/2015.
// Copyright (c) 2015 Syrup Apps. All rights reserved.
//
#import "AVAudioPlayer+AVAudioPlayer_Fading.h"
typedef void(^fadeCompletion)(BOOL);
@implementation AVAudioPlayer (AVAudioPlayer_Fading)
-(void)fadeOutWithCompletion:(void(^)(BOOL finished))completion {
if (self.volume > 0.1) {
self.volume = self.volume - 0.1;
[self performSelector:@selector(fadeOutWithCompletion:) withObject:completion afterDelay:0.05f];
} else {
completion(YES);
}
}
-(void)fadeInWithCompletion:(void(^)(BOOL finished))completion {
if (self.volume < 0.9) {
self.volume = self.volume + 0.1;
[self performSelector:@selector(fadeOutWithCompletion:) withObject:completion afterDelay:0.05f];
} else {
completion(YES);
}
}
- (void)fadeOutPause{
NSTimeInterval trueTime = self.currentTime;
[self fadeOutWithCompletion:^(BOOL finished) {
[self pause];
[self prepareToPlay];
self.volume = 1.0;
self.currentTime = trueTime;
}];
}
- (void)fadeInPlay {
[self prepareToPlay];
[self play];
[self fadeInWithCompletion:^(BOOL finished) {
self.volume = 1.0;
}];
}
-(void)fadeOutAndStop{
[self fadeOutWithCompletion:^(BOOL stop) {
// Stop and get the sound ready for playing again
self.volume = 1.0;
self.currentTime = 0;
[self stop];
}];
}
- (void)setTrueCurrentTime:(id)trueCurrentTime{
objc_setAssociatedObject(self, @selector(trueCurrentTime), trueCurrentTime, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)trueCurrentTime {
return objc_getAssociatedObject(self, @selector(trueCurrentTime));
}
@end