|
1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 * |
|
18 * File created on 13/03/2011. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "AudioManagerController.h" |
|
23 #import "AVFoundation/AVAudioPlayer.h" |
|
24 #import <AudioToolbox/AudioToolbox.h> |
|
25 |
|
26 |
|
27 static AVAudioPlayer *backgroundMusic = nil; |
|
28 static SystemSoundID clickSound = -1; |
|
29 static SystemSoundID backSound = -1; |
|
30 static SystemSoundID selSound = -1; |
|
31 |
|
32 @implementation AudioManagerController |
|
33 |
|
34 #pragma mark - |
|
35 #pragma mark background music control |
|
36 +(void) loadBackgroundMusic { |
|
37 NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; |
|
38 backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; |
|
39 |
|
40 backgroundMusic.delegate = nil; |
|
41 backgroundMusic.volume = 0.4f; |
|
42 backgroundMusic.numberOfLoops = -1; |
|
43 [backgroundMusic prepareToPlay]; |
|
44 } |
|
45 |
|
46 +(void) playBackgroundMusic { |
|
47 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO) |
|
48 return; |
|
49 |
|
50 if (backgroundMusic == nil) |
|
51 [AudioManagerController loadBackgroundMusic]; |
|
52 |
|
53 [backgroundMusic play]; |
|
54 } |
|
55 |
|
56 +(void) pauseBackgroundMusic { |
|
57 [backgroundMusic pause]; |
|
58 } |
|
59 |
|
60 +(void) stopBackgroundMusic { |
|
61 [backgroundMusic stop]; |
|
62 } |
|
63 |
|
64 #pragma mark - |
|
65 #pragma mark sound effects control |
|
66 +(SystemSoundID) loadSound:(NSString *)snd { |
|
67 // get the filename of the sound file: |
|
68 NSString *path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd]; |
|
69 |
|
70 // declare a system sound id and get a URL for the sound file |
|
71 SystemSoundID soundID; |
|
72 NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; |
|
73 |
|
74 // use audio sevices to create and play the sound |
|
75 AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); |
|
76 return soundID; |
|
77 } |
|
78 |
|
79 +(void) playClickSound { |
|
80 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) |
|
81 return; |
|
82 |
|
83 if (clickSound == -1) |
|
84 clickSound = [AudioManagerController loadSound:@"clickSound.wav"]; |
|
85 |
|
86 AudioServicesPlaySystemSound(clickSound); |
|
87 } |
|
88 |
|
89 +(void) playBackSound { |
|
90 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) |
|
91 return; |
|
92 |
|
93 if (backSound == -1) |
|
94 backSound = [AudioManagerController loadSound:@"backSound.wav"]; |
|
95 |
|
96 AudioServicesPlaySystemSound(backSound); |
|
97 } |
|
98 |
|
99 +(void) playSelectSound { |
|
100 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) |
|
101 return; |
|
102 |
|
103 if (selSound == -1) |
|
104 selSound = [AudioManagerController loadSound:@"selSound.wav"]; |
|
105 |
|
106 AudioServicesPlaySystemSound(selSound); |
|
107 } |
|
108 |
|
109 #pragma mark - |
|
110 #pragma mark memory management |
|
111 +(void) didReceiveMemoryWarning { |
|
112 [backgroundMusic stop]; |
|
113 backgroundMusic = nil; |
|
114 clickSound = -1; |
|
115 backSound = -1; |
|
116 } |
|
117 |
|
118 +(void) dealloc { |
|
119 releaseAndNil(backgroundMusic); |
|
120 AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1; |
|
121 AudioServicesDisposeSystemSoundID(backSound), backSound = -1; |
|
122 AudioServicesDisposeSystemSoundID(selSound), selSound = -1; |
|
123 [super dealloc]; |
|
124 } |
|
125 |
|
126 @end |