author | Wuzzy <almikes@aol.com> |
Wed, 13 Apr 2016 00:17:07 +0200 | |
changeset 11716 | c7cdd98bb1ec |
parent 11137 | 14f50dde3e8c |
child 12872 | 00215a7ec5f5 |
permissions | -rw-r--r-- |
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
1 |
// MXAudioPlayerFadeOperation.m |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
2 |
// |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
3 |
// Created by Andrew Mackenzie-Ross on 30/11/10. |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
4 |
// mackross.net. |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
5 |
// |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
6 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
7 |
#import "MXAudioPlayerFadeOperation.h" |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
8 |
#import <AVFoundation/AVFoundation.h> |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
9 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
10 |
#define SKVolumeChangesPerSecond 15 |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
11 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
12 |
@interface MXAudioPlayerFadeOperation () |
8441 | 13 |
@property (nonatomic, retain, readwrite) AVAudioPlayer *audioPlayer; |
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
14 |
- (void)beginFadeOperation; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
15 |
- (void)finishFadeOperation; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
16 |
@end |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
17 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
18 |
@implementation MXAudioPlayerFadeOperation |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
19 |
#pragma mark - |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
20 |
#pragma mark Properties |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
21 |
@synthesize audioPlayer = _audioPlayer; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
22 |
@synthesize fadeDuration = _fadeDuration; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
23 |
@synthesize finishVolume = _finishVolume; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
24 |
@synthesize playBeforeFade = _playBeforeFade; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
25 |
@synthesize pauseAfterFade = _pauseAfterFade; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
26 |
@synthesize stopAfterFade = _stopAfterFade; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
27 |
@synthesize delay = _delay; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
28 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
29 |
#pragma mark - |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
30 |
#pragma mark Accessors |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
31 |
- (AVAudioPlayer *)audioPlayer { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
32 |
AVAudioPlayer *result; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
33 |
@synchronized(self) { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
34 |
result = [_audioPlayer retain]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
35 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
36 |
return [result autorelease]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
37 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
38 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
39 |
- (void)setAudioPlayer:(AVAudioPlayer *)anAudioPlayer { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
40 |
@synchronized(self) { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
41 |
if (_audioPlayer != anAudioPlayer) { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
42 |
[_audioPlayer release]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
43 |
_audioPlayer = [anAudioPlayer retain]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
44 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
45 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
46 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
47 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
48 |
#pragma mark - |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
49 |
#pragma mark NSOperation |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
50 |
-(id) initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume overDuration:(NSTimeInterval)duration withDelay:(NSTimeInterval)timeDelay { |
6908
896ed2afcfb8
ios: turn on more warning messages and start correcting them
koda
parents:
6659
diff
changeset
|
51 |
if ((self = [super init])) { |
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
52 |
self.audioPlayer = player; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
53 |
[player prepareToPlay]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
54 |
_fadeDuration = duration; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
55 |
_finishVolume = volume; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
56 |
_playBeforeFade = YES; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
57 |
_stopAfterFade = NO; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
58 |
_pauseAfterFade = (volume == 0.0) ? YES : NO; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
59 |
_delay = timeDelay; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
60 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
61 |
return self; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
62 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
63 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
64 |
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume overDuration:(NSTimeInterval)duration { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
65 |
return [self initFadeWithAudioPlayer:player toVolume:volume overDuration:duration withDelay:0.0]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
66 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
67 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
68 |
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
69 |
return [self initFadeWithAudioPlayer:player toVolume:volume overDuration:1.0]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
70 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
71 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
72 |
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
73 |
return [self initFadeWithAudioPlayer:player toVolume:0.0]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
74 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
75 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
76 |
- (id) init { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
77 |
ALog(@"Failed to init class (%@) with AVAudioPlayer instance, use initFadeWithAudioPlayer:",[self class]); |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
78 |
return nil; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
79 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
80 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
81 |
- (void)main { |
11137
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
8441
diff
changeset
|
82 |
@autoreleasepool { |
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
8441
diff
changeset
|
83 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
84 |
[NSThread sleepForTimeInterval:_delay]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
85 |
if ([self.audioPlayer isKindOfClass:[AVAudioPlayer class]]) { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
86 |
[self beginFadeOperation]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
87 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
88 |
else { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
89 |
ALog(@"AudioPlayerFadeOperation began with invalid AVAudioPlayer"); |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
90 |
} |
8441 | 91 |
|
11137
14f50dde3e8c
- Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents:
8441
diff
changeset
|
92 |
} |
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
93 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
94 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
95 |
- (void)beginFadeOperation { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
96 |
if (![self.audioPlayer isPlaying] && _playBeforeFade) [self.audioPlayer play]; |
8441 | 97 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
98 |
if (_fadeDuration != 0.0) { |
8441 | 99 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
100 |
NSTimeInterval sleepInterval = (1.0 / SKVolumeChangesPerSecond); |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
101 |
NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
102 |
NSTimeInterval now = startTime; |
8441 | 103 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
104 |
float startVolume = [self.audioPlayer volume]; |
8441 | 105 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
106 |
while (now < (startTime + _fadeDuration)) { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
107 |
float ratioOfFadeCompleted = (now - startTime)/_fadeDuration; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
108 |
float volume = (_finishVolume * ratioOfFadeCompleted) + (startVolume * (1-ratioOfFadeCompleted)); |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
109 |
[self.audioPlayer setVolume:volume]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
110 |
[NSThread sleepForTimeInterval:sleepInterval]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
111 |
now = [[NSDate date] timeIntervalSinceReferenceDate]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
112 |
} |
8441 | 113 |
|
6656
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
114 |
[self.audioPlayer setVolume:_finishVolume]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
115 |
[self finishFadeOperation]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
116 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
117 |
else { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
118 |
[self.audioPlayer setVolume:_finishVolume]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
119 |
[self finishFadeOperation]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
120 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
121 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
122 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
123 |
- (void)finishFadeOperation { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
124 |
if ([self.audioPlayer isPlaying] && _pauseAfterFade) [self.audioPlayer pause]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
125 |
if ([self.audioPlayer isPlaying] && _stopAfterFade) [self.audioPlayer stop]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
126 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
127 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
128 |
- (void)dealloc { |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
129 |
releaseAndNil(_audioPlayer); |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
130 |
[super dealloc]; |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
131 |
} |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
132 |
|
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
133 |
@end |
6aeaba3ee584
ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff
changeset
|
134 |