project_files/HedgewarsMobile/Classes/AudioManagerController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 10108 c68cf030eded
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    35     if (mainInstance == nil)
    35     if (mainInstance == nil)
    36         mainInstance = [[self alloc] init];
    36         mainInstance = [[self alloc] init];
    37     return mainInstance;
    37     return mainInstance;
    38 }
    38 }
    39 
    39 
    40 -(id) init {
    40 - (id)init {
    41     if ((self = [super init])) {
    41     if ((self = [super init])) {
    42         self.backgroundMusic = nil;
    42         self.backgroundMusic = nil;
    43         self.clickSound = -1;
    43         self.clickSound = -1;
    44         self.backSound = -1;
    44         self.backSound = -1;
    45         self.selSound = -1;
    45         self.selSound = -1;
    47         self.audioFaderQueue = nil;
    47         self.audioFaderQueue = nil;
    48     }
    48     }
    49     return self;
    49     return self;
    50 }
    50 }
    51 
    51 
    52 -(void) dealloc {
    52 - (void)dealloc {
    53     [self unloadSounds];
    53     [self unloadSounds];
    54     releaseAndNil(backgroundMusic);
       
    55     releaseAndNil(audioFaderQueue);
       
    56     mainInstance = nil;
    54     mainInstance = nil;
    57     [super dealloc];
       
    58 }
    55 }
    59 
    56 
    60 -(void) didReceiveMemoryWarning {
    57 - (void)didReceiveMemoryWarning {
    61     if (self.backgroundMusic.playing == NO)
    58     if (self.backgroundMusic.playing == NO)
    62         self.backgroundMusic = nil;
    59         self.backgroundMusic = nil;
    63     if ([self.audioFaderQueue operationCount] == 0)
    60     if ([self.audioFaderQueue operationCount] == 0)
    64         self.audioFaderQueue = nil;
    61         self.audioFaderQueue = nil;
    65 
    62 
    67     MSG_MEMCLEAN();
    64     MSG_MEMCLEAN();
    68 }
    65 }
    69 
    66 
    70 #pragma mark -
    67 #pragma mark -
    71 #pragma mark background music control
    68 #pragma mark background music control
    72 -(void) playBackgroundMusic {
    69 - (void)playBackgroundMusic {
    73     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    70     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    74         return;
    71         return;
    75 
    72 
    76     if (self.backgroundMusic == nil) {
    73     if (self.backgroundMusic == nil) {
    77         NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
    74         NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
    82 
    79 
    83     self.backgroundMusic.volume = DEFAULT_VOLUME;
    80     self.backgroundMusic.volume = DEFAULT_VOLUME;
    84     [self.backgroundMusic play];
    81     [self.backgroundMusic play];
    85 }
    82 }
    86 
    83 
    87 -(void) pauseBackgroundMusic {
    84 - (void)pauseBackgroundMusic {
    88     [self.backgroundMusic pause];
    85     [self.backgroundMusic pause];
    89 }
    86 }
    90 
    87 
    91 -(void) stopBackgroundMusic {
    88 - (void)stopBackgroundMusic {
    92     [self.backgroundMusic stop];
    89     [self.backgroundMusic stop];
    93 }
    90 }
    94 
    91 
    95 -(void) fadeOutBackgroundMusic {
    92 - (void)fadeOutBackgroundMusic {
    96     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    93     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    97         return;
    94         return;
    98 
    95 
    99     if (self.audioFaderQueue == nil)
    96     if (self.audioFaderQueue == nil)
   100         self.audioFaderQueue = [[NSOperationQueue alloc] init];
    97         self.audioFaderQueue = [[NSOperationQueue alloc] init];
   101 
    98 
   102     MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
    99     MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
   103                                                                                              toVolume:0.0
   100                                                                                              toVolume:0.0
   104                                                                                          overDuration:FADEOUT_DURATION];
   101                                                                                          overDuration:FADEOUT_DURATION];
   105     [self.audioFaderQueue addOperation:fadeOut];
   102     [self.audioFaderQueue addOperation:fadeOut];
   106     [fadeOut release];
       
   107 }
   103 }
   108 
   104 
   109 -(void) fadeInBackgroundMusic {
   105 - (void)fadeInBackgroundMusic {
   110     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
   106     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
   111         return;
   107         return;
   112 
   108 
   113     if (self.audioFaderQueue == nil)
   109     if (self.audioFaderQueue == nil)
   114         self.audioFaderQueue = [[NSOperationQueue alloc] init];
   110         self.audioFaderQueue = [[NSOperationQueue alloc] init];
   116     [self playBackgroundMusic];
   112     [self playBackgroundMusic];
   117     MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
   113     MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
   118                                                                                             toVolume:DEFAULT_VOLUME
   114                                                                                             toVolume:DEFAULT_VOLUME
   119                                                                                         overDuration:FADEIN_DURATION];
   115                                                                                         overDuration:FADEIN_DURATION];
   120     [audioFaderQueue addOperation:fadeIn];
   116     [audioFaderQueue addOperation:fadeIn];
   121     [fadeIn release];
       
   122 }
   117 }
   123 
   118 
   124 #pragma mark -
   119 #pragma mark -
   125 #pragma mark sound effects control
   120 #pragma mark sound effects control
   126 -(SystemSoundID) loadSound:(NSString *)snd {
   121 -(SystemSoundID) loadSound:(NSString *)snd {
   129     // get the filename of the sound file in a NSURL format
   124     // get the filename of the sound file in a NSURL format
   130     NSString *path = [[NSBundle mainBundle] pathForResource:snd ofType:@"caf"];
   125     NSString *path = [[NSBundle mainBundle] pathForResource:snd ofType:@"caf"];
   131     NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
   126     NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
   132 
   127 
   133     // use audio sevices to create and play the sound
   128     // use audio sevices to create and play the sound
   134     AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
   129     AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
   135     return soundID;
   130     return soundID;
   136 }
   131 }
   137 
   132 
   138 -(void) unloadSounds {
   133 - (void)unloadSounds {
   139     AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
   134     AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
   140     AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
   135     AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
   141     AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
   136     AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
   142 }
   137 }
   143 
   138 
   144 -(void) playClickSound {
   139 - (void)playClickSound {
   145     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   140     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   146         return;
   141         return;
   147 
   142 
   148     if (self.clickSound == -1)
   143     if (self.clickSound == -1)
   149         self.clickSound = [self loadSound:@"clickSound"];
   144         self.clickSound = [self loadSound:@"clickSound"];
   150 
   145 
   151     AudioServicesPlaySystemSound(self.clickSound);
   146     AudioServicesPlaySystemSound(self.clickSound);
   152 }
   147 }
   153 
   148 
   154 -(void) playBackSound {
   149 - (void)playBackSound {
   155     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   150     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   156         return;
   151         return;
   157 
   152 
   158     if (self.backSound == -1)
   153     if (self.backSound == -1)
   159         self.backSound = [self loadSound:@"backSound"];
   154         self.backSound = [self loadSound:@"backSound"];
   160 
   155 
   161     AudioServicesPlaySystemSound(self.backSound);
   156     AudioServicesPlaySystemSound(self.backSound);
   162 }
   157 }
   163 
   158 
   164 -(void) playSelectSound {
   159 - (void)playSelectSound {
   165     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   160     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   166         return;
   161         return;
   167 
   162 
   168     if (self.selSound == -1)
   163     if (self.selSound == -1)
   169         self.selSound = [self loadSound:@"selSound"];
   164         self.selSound = [self loadSound:@"selSound"];