20 |
20 |
21 |
21 |
22 #import "AudioManagerController.h" |
22 #import "AudioManagerController.h" |
23 #import "AVFoundation/AVAudioPlayer.h" |
23 #import "AVFoundation/AVAudioPlayer.h" |
24 #import <AudioToolbox/AudioToolbox.h> |
24 #import <AudioToolbox/AudioToolbox.h> |
25 |
25 #import "MXAudioPlayerFadeOperation.h" |
26 |
26 |
27 static AVAudioPlayer *backgroundMusic = nil; |
27 static AVAudioPlayer *backgroundMusic = nil; |
28 static SystemSoundID clickSound = -1; |
28 static SystemSoundID clickSound = -1; |
29 static SystemSoundID backSound = -1; |
29 static SystemSoundID backSound = -1; |
30 static SystemSoundID selSound = -1; |
30 static SystemSoundID selSound = -1; |
|
31 |
|
32 static NSOperationQueue *audioFaderQueue = nil; |
|
33 static MXAudioPlayerFadeOperation *fadeIn = nil; |
|
34 static MXAudioPlayerFadeOperation *fadeOut = nil; |
|
35 |
31 |
36 |
32 @implementation AudioManagerController |
37 @implementation AudioManagerController |
33 |
38 |
34 #pragma mark - |
39 #pragma mark - |
35 #pragma mark background music control |
40 #pragma mark background music control |
36 +(void) loadBackgroundMusic { |
41 +(void) loadBackgroundMusic { |
37 NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; |
42 NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; |
38 backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; |
43 backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; |
39 |
44 |
40 backgroundMusic.delegate = nil; |
45 backgroundMusic.delegate = nil; |
41 backgroundMusic.volume = 0.4f; |
46 backgroundMusic.volume = 0; |
42 backgroundMusic.numberOfLoops = -1; |
47 backgroundMusic.numberOfLoops = -1; |
43 [backgroundMusic prepareToPlay]; |
48 [backgroundMusic prepareToPlay]; |
44 } |
49 } |
45 |
50 |
46 +(void) playBackgroundMusic { |
51 +(void) playBackgroundMusic { |
48 return; |
53 return; |
49 |
54 |
50 if (backgroundMusic == nil) |
55 if (backgroundMusic == nil) |
51 [AudioManagerController loadBackgroundMusic]; |
56 [AudioManagerController loadBackgroundMusic]; |
52 |
57 |
|
58 backgroundMusic.volume = 0.45f; |
53 [backgroundMusic play]; |
59 [backgroundMusic play]; |
54 } |
60 } |
55 |
61 |
56 +(void) pauseBackgroundMusic { |
62 +(void) pauseBackgroundMusic { |
57 [backgroundMusic pause]; |
63 [backgroundMusic pause]; |
58 } |
64 } |
59 |
65 |
60 +(void) stopBackgroundMusic { |
66 +(void) stopBackgroundMusic { |
61 [backgroundMusic stop]; |
67 [backgroundMusic stop]; |
|
68 } |
|
69 |
|
70 +(void) fadeOutBackgroundMusic { |
|
71 if (audioFaderQueue == nil) |
|
72 audioFaderQueue = [[NSOperationQueue alloc] init]; |
|
73 if (backgroundMusic == nil) |
|
74 [AudioManagerController loadBackgroundMusic]; |
|
75 if (fadeOut == nil) |
|
76 fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.0 overDuration:3.0]; |
|
77 |
|
78 [audioFaderQueue addOperation:fadeOut]; |
|
79 } |
|
80 |
|
81 +(void) fadeInBackgroundMusic { |
|
82 if (audioFaderQueue == nil) |
|
83 audioFaderQueue = [[NSOperationQueue alloc] init]; |
|
84 if (backgroundMusic == nil) |
|
85 [AudioManagerController loadBackgroundMusic]; |
|
86 if (fadeIn == nil) |
|
87 fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.45 overDuration:2.0]; |
|
88 |
|
89 [audioFaderQueue addOperation:fadeIn]; |
62 } |
90 } |
63 |
91 |
64 #pragma mark - |
92 #pragma mark - |
65 #pragma mark sound effects control |
93 #pragma mark sound effects control |
66 +(SystemSoundID) loadSound:(NSString *)snd { |
94 +(SystemSoundID) loadSound:(NSString *)snd { |
109 #pragma mark - |
137 #pragma mark - |
110 #pragma mark memory management |
138 #pragma mark memory management |
111 +(void) releaseCache { |
139 +(void) releaseCache { |
112 [backgroundMusic stop]; |
140 [backgroundMusic stop]; |
113 [backgroundMusic release], backgroundMusic = nil; |
141 [backgroundMusic release], backgroundMusic = nil; |
|
142 [fadeOut release], fadeOut = nil; |
|
143 [fadeIn release], fadeIn = nil; |
|
144 [audioFaderQueue release], audioFaderQueue = nil; |
114 AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1; |
145 AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1; |
115 AudioServicesDisposeSystemSoundID(backSound), backSound = -1; |
146 AudioServicesDisposeSystemSoundID(backSound), backSound = -1; |
116 AudioServicesDisposeSystemSoundID(selSound), selSound = -1; |
147 AudioServicesDisposeSystemSoundID(selSound), selSound = -1; |
117 MSG_MEMCLEAN(); |
148 MSG_MEMCLEAN(); |
118 } |
149 } |