--- a/project_files/HedgewarsMobile/Classes/AudioManagerController.m Mon Apr 09 02:02:14 2012 +0200
+++ b/project_files/HedgewarsMobile/Classes/AudioManagerController.m Mon Apr 09 03:25:17 2012 +0200
@@ -19,137 +19,157 @@
#import "AudioManagerController.h"
#import "AVFoundation/AVAudioPlayer.h"
-#import <AudioToolbox/AudioToolbox.h>
#import "MXAudioPlayerFadeOperation.h"
-static AVAudioPlayer *backgroundMusic = nil;
-static SystemSoundID clickSound = -1;
-static SystemSoundID backSound = -1;
-static SystemSoundID selSound = -1;
+#define DEFAULT_VOLUME 0.45f
+#define FADEOUT_DURATION 3.0f
+#define FADEIN_DURATION 2.0f
-static NSOperationQueue *audioFaderQueue = nil;
-static MXAudioPlayerFadeOperation *fadeIn = nil;
-static MXAudioPlayerFadeOperation *fadeOut = nil;
+static AudioManagerController *mainInstance;
@implementation AudioManagerController
+@synthesize backgroundMusic, clickSound, backSound, selSound, audioFaderQueue;
+
++(id) mainManager {
+ if (mainInstance == nil)
+ mainInstance = [[self alloc] init];
+ return mainInstance;
+}
+
+-(id) init {
+ if (self = [super init]) {
+ self.backgroundMusic = nil;
+ self.clickSound = -1;
+ self.backSound = -1;
+ self.selSound = -1;
+
+ self.audioFaderQueue = nil;
+ }
+ return self;
+}
+
+-(void) dealloc {
+ [self unloadSounds];
+ releaseAndNil(backgroundMusic);
+ releaseAndNil(audioFaderQueue);
+ mainInstance = nil;
+ [super dealloc];
+}
+
+-(void) didReceiveMemoryWarning {
+ if (self.backgroundMusic.playing == NO)
+ self.backgroundMusic = nil;
+ if ([self.audioFaderQueue operationCount] == 0)
+ self.audioFaderQueue = nil;
+
+ [self unloadSounds];
+ MSG_MEMCLEAN();
+}
#pragma mark -
#pragma mark background music control
-+(void) loadBackgroundMusic {
- NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
- backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil];
-
- backgroundMusic.delegate = nil;
- backgroundMusic.volume = 0;
- backgroundMusic.numberOfLoops = -1;
- [backgroundMusic prepareToPlay];
-}
-
-+(void) playBackgroundMusic {
+-(void) playBackgroundMusic {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
return;
- if (backgroundMusic == nil)
- [AudioManagerController loadBackgroundMusic];
-
- backgroundMusic.volume = 0.45f;
- [backgroundMusic play];
+ if (self.backgroundMusic == nil) {
+ NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
+ self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil];
+ self.backgroundMusic.delegate = nil;
+ self.backgroundMusic.numberOfLoops = -1;
+ }
+
+ self.backgroundMusic.volume = DEFAULT_VOLUME;
+ [self.backgroundMusic play];
}
-+(void) pauseBackgroundMusic {
- [backgroundMusic pause];
+-(void) pauseBackgroundMusic {
+ [self.backgroundMusic pause];
}
-+(void) stopBackgroundMusic {
- [backgroundMusic stop];
+-(void) stopBackgroundMusic {
+ [self.backgroundMusic stop];
}
-+(void) fadeOutBackgroundMusic {
+-(void) fadeOutBackgroundMusic {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
return;
- if (audioFaderQueue == nil)
- audioFaderQueue = [[NSOperationQueue alloc] init];
- if (backgroundMusic == nil)
- [AudioManagerController loadBackgroundMusic];
- if (fadeOut == nil)
- fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.0 overDuration:3.0];
-
- [audioFaderQueue addOperation:fadeOut];
+ if (self.audioFaderQueue == nil)
+ self.audioFaderQueue = [[NSOperationQueue alloc] init];
+
+ MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
+ toVolume:0.0
+ overDuration:FADEOUT_DURATION];
+ [self.audioFaderQueue addOperation:fadeOut];
+ [fadeOut release];
}
-+(void) fadeInBackgroundMusic {
+-(void) fadeInBackgroundMusic {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
return;
- if (audioFaderQueue == nil)
- audioFaderQueue = [[NSOperationQueue alloc] init];
- if (backgroundMusic == nil)
- [AudioManagerController loadBackgroundMusic];
- if (fadeIn == nil)
- fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.45 overDuration:2.0];
+ if (self.audioFaderQueue == nil)
+ self.audioFaderQueue = [[NSOperationQueue alloc] init];
+ [self playBackgroundMusic];
+ MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
+ toVolume:DEFAULT_VOLUME
+ overDuration:FADEIN_DURATION];
[audioFaderQueue addOperation:fadeIn];
+ [fadeIn release];
}
#pragma mark -
#pragma mark sound effects control
-+(SystemSoundID) loadSound:(NSString *)snd {
- // get the filename of the sound file:
- NSString *path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd];
+-(SystemSoundID) loadSound:(NSString *)snd {
+ SystemSoundID soundID;
- // declare a system sound id and get a URL for the sound file
- SystemSoundID soundID;
+ // get the filename of the sound file in a NSURL format
+ NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
+ [path release];
// use audio sevices to create and play the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
return soundID;
}
-+(void) playClickSound {
- if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
- return;
-
- if (clickSound == -1)
- clickSound = [AudioManagerController loadSound:@"clickSound.wav"];
-
- AudioServicesPlaySystemSound(clickSound);
+-(void) unloadSounds {
+ AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
+ AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
+ AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
}
-+(void) playBackSound {
+-(void) playClickSound {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
return;
- if (backSound == -1)
- backSound = [AudioManagerController loadSound:@"backSound.wav"];
+ if (self.clickSound == -1)
+ self.clickSound = [self loadSound:@"clickSound.wav"];
- AudioServicesPlaySystemSound(backSound);
+ AudioServicesPlaySystemSound(self.clickSound);
}
-+(void) playSelectSound {
+-(void) playBackSound {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
return;
- if (selSound == -1)
- selSound = [AudioManagerController loadSound:@"selSound.wav"];
+ if (self.backSound == -1)
+ self.backSound = [self loadSound:@"backSound.wav"];
- AudioServicesPlaySystemSound(selSound);
+ AudioServicesPlaySystemSound(self.backSound);
}
-#pragma mark -
-#pragma mark memory management
-+(void) releaseCache {
- [backgroundMusic stop];
- [backgroundMusic release], backgroundMusic = nil;
- [fadeOut release], fadeOut = nil;
- [fadeIn release], fadeIn = nil;
- [audioFaderQueue release], audioFaderQueue = nil;
- AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
- AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
- AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
- MSG_MEMCLEAN();
+-(void) playSelectSound {
+ if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
+ return;
+
+ if (self.selSound == -1)
+ self.selSound = [self loadSound:@"selSound.wav"];
+
+ AudioServicesPlaySystemSound(self.selSound);
}
@end