Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.audio; |
| 2 | |||
| 3 | import java.util.HashMap; |
||
| 4 | import java.util.Map; |
||
| 5 | |||
| 6 | import com.badlogic.gdx.Gdx; |
||
| 7 | import com.badlogic.gdx.audio.Music; |
||
| 8 | import com.badlogic.gdx.audio.Sound; |
||
| 9 | import com.gebauz.bauzoid.file.FileUtil; |
||
| 10 | import com.gebauz.bauzoid.game.Game; |
||
| 11 | import com.gebauz.bauzoid.game.GameObject; |
||
| 12 | |||
| 13 | public class Audio extends GameObject |
||
| 14 | { |
||
| 15 | private static final String PREF_MUSIC_ENABLED = "music.enabled"; |
||
| 16 | private static final String PREF_SOUND_ENABLED = "sound.enabled"; |
||
| 17 | |||
| 18 | private boolean mSoundEnabled = true; |
||
| 19 | private boolean mMusicEnabled = true; |
||
| 20 | private boolean mMusicLooping = false; |
||
| 21 | |||
| 22 | //private Vector<Sound> mSounds = new Vector<Sound>(); |
||
| 23 | private HashMap<String, Sound> mSounds = new HashMap<String, Sound>(); |
||
| 24 | private Music mMusic = null; |
||
| 25 | |||
| 26 | public Audio(Game game) |
||
| 27 | { |
||
| 28 | super(game); |
||
| 29 | } |
||
| 30 | |||
| 31 | public void init() |
||
| 32 | { |
||
| 33 | mSoundEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_SOUND_ENABLED, true); |
||
| 34 | mMusicEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_MUSIC_ENABLED, true); |
||
| 35 | } |
||
| 36 | |||
| 37 | public void exit() |
||
| 38 | { |
||
| 39 | clearSounds(); |
||
| 40 | if (mMusic != null) |
||
| 41 | { |
||
| 42 | mMusic.dispose(); |
||
| 43 | mMusic = null; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | public void update(float deltaTime) |
||
| 48 | { |
||
| 49 | |||
| 50 | } |
||
| 51 | |||
| 52 | public void render() |
||
| 53 | { |
||
| 54 | |||
| 55 | } |
||
| 56 | |||
| 57 | public void onPause() |
||
| 58 | { |
||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | public void onResume() |
||
| 63 | { |
||
| 64 | |||
| 65 | } |
||
| 66 | |||
| 67 | public void clearSounds() |
||
| 68 | { |
||
| 69 | /*for (int i = 0; i < mSounds.size(); i++) |
||
| 70 | { |
||
| 71 | mSounds.get(i).dispose(); |
||
| 72 | }*/ |
||
| 73 | |||
| 74 | for (Map.Entry<String, Sound> entry : mSounds.entrySet()) |
||
| 75 | { |
||
| 76 | entry.getValue().dispose(); |
||
| 77 | } |
||
| 78 | mSounds.clear(); |
||
| 79 | } |
||
| 80 | |||
| 81 | public Sound newManagedSound(String filename) |
||
| 82 | { |
||
| 83 | String soundName = FileUtil.extractFilenameNoExt(filename); |
||
| 84 | |||
| 85 | if (mSounds.containsKey(soundName)) |
||
| 86 | return mSounds.get(soundName); |
||
| 87 | |||
| 88 | Sound sound = Gdx.audio.newSound(Gdx.files.internal(filename)); |
||
| 89 | //mSounds.add(sound); |
||
| 90 | mSounds.put(soundName, sound); |
||
| 91 | |||
| 92 | return sound; |
||
| 93 | } |
||
| 94 | |||
| 95 | public void removeManagedSound(Sound sound) |
||
| 96 | { |
||
| 97 | if (sound == null) |
||
| 98 | return; |
||
| 99 | |||
| 100 | while (mSounds.values().remove(sound)); |
||
| 101 | |||
| 102 | /*int i = mSounds.indexOf(sound; |
||
| 103 | if (i != -1) |
||
| 104 | { |
||
| 105 | mSounds.remove(i); |
||
| 106 | }*/ |
||
| 107 | |||
| 108 | // regardless of whether in list, dispose |
||
| 109 | sound.dispose(); |
||
| 110 | } |
||
| 111 | |||
| 112 | public void loadMusic(String filename) |
||
| 113 | { |
||
| 114 | if (mMusic != null) |
||
| 115 | { |
||
| 116 | mMusic.stop(); |
||
| 117 | mMusic.dispose(); |
||
| 118 | mMusic = null; |
||
| 119 | } |
||
| 120 | mMusic = Gdx.audio.newMusic(Gdx.files.internal(filename)); |
||
| 121 | } |
||
| 122 | |||
| 123 | public void disposeMusic() |
||
| 124 | { |
||
| 125 | if (mMusic != null) |
||
| 126 | { |
||
| 127 | mMusic.stop(); |
||
| 128 | mMusic.dispose(); |
||
| 129 | mMusic = null; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | public void playMusic(String filename, boolean looping, float volume) |
||
| 134 | { |
||
| 135 | loadMusic(filename); |
||
| 136 | setMusicVolume(volume); |
||
| 137 | playMusic(looping); |
||
| 138 | } |
||
| 139 | |||
| 140 | public void playMusic(String filename, boolean looping) |
||
| 141 | { |
||
| 142 | loadMusic(filename); |
||
| 143 | playMusic(looping); |
||
| 144 | } |
||
| 145 | |||
| 146 | public void playMusic(boolean looping) |
||
| 147 | { |
||
| 148 | if (isMusicEnabled() && (mMusic != null)) |
||
| 149 | { |
||
| 150 | mMusicLooping = looping; |
||
| 151 | mMusic.setLooping(looping); |
||
| 152 | mMusic.play(); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | public void pauseMusic() |
||
| 157 | { |
||
| 158 | if (mMusic != null) |
||
| 159 | { |
||
| 160 | mMusic.pause(); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | public void stopMusic() |
||
| 165 | { |
||
| 166 | if (mMusic != null) |
||
| 167 | { |
||
| 168 | mMusic.stop(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | public boolean isMusicPlaying() |
||
| 173 | { |
||
| 174 | if (mMusic == null) |
||
| 175 | return false; |
||
| 176 | |||
| 177 | return mMusic.isPlaying(); |
||
| 178 | } |
||
| 179 | |||
| 180 | public void setMusicVolume(float volume) |
||
| 181 | { |
||
| 182 | if (mMusic != null) |
||
| 183 | { |
||
| 184 | mMusic.setVolume(volume); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | public void playSound(Sound sound) |
||
| 189 | { |
||
| 190 | if (!isSoundEnabled()) |
||
| 191 | return; |
||
| 192 | |||
| 193 | sound.play(); |
||
| 194 | } |
||
| 195 | |||
| 196 | public void playSound(Sound sound, float volume) |
||
| 197 | { |
||
| 198 | if (!isSoundEnabled()) |
||
| 199 | return; |
||
| 200 | |||
| 201 | sound.play(volume); |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | public void playSound(Sound sound, float volume, float pitch, float pan) |
||
| 206 | { |
||
| 207 | if (!isSoundEnabled()) |
||
| 208 | return; |
||
| 209 | |||
| 210 | sound.play(volume, pitch, pan); |
||
| 211 | } |
||
| 212 | |||
| 213 | public final boolean isSoundEnabled() |
||
| 214 | { |
||
| 215 | return mSoundEnabled; |
||
| 216 | } |
||
| 217 | |||
| 218 | public final boolean isMusicEnabled() |
||
| 219 | { |
||
| 220 | return mMusicEnabled; |
||
| 221 | } |
||
| 222 | |||
| 223 | public final void setSoundEnabled(boolean enabled) |
||
| 224 | { |
||
| 225 | mSoundEnabled = enabled; |
||
| 226 | |||
| 227 | // stop sounds playing |
||
| 228 | if (!isSoundEnabled()) |
||
| 229 | { |
||
| 230 | /*for (int i = 0; i < mSounds.size(); i++) |
||
| 231 | { |
||
| 232 | mSounds.get(i).stop(); |
||
| 233 | }*/ |
||
| 234 | |||
| 235 | for (Map.Entry<String, Sound> entry : mSounds.entrySet()) |
||
| 236 | { |
||
| 237 | entry.getValue().stop(); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled); |
||
| 242 | Gdx.app.getPreferences(getGame().getGameTitle()).flush(); |
||
| 243 | } |
||
| 244 | |||
| 245 | public final void setMusicEnabled(boolean enabled) |
||
| 246 | { |
||
| 247 | mMusicEnabled = enabled; |
||
| 248 | |||
| 249 | if (isMusicEnabled()) |
||
| 250 | { |
||
| 251 | playMusic(mMusicLooping); |
||
| 252 | } |
||
| 253 | else |
||
| 254 | { |
||
| 255 | pauseMusic(); |
||
| 256 | } |
||
| 257 | |||
| 258 | Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled); |
||
| 259 | Gdx.app.getPreferences(getGame().getGameTitle()).flush(); |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | public Sound getSound(String soundName) |
||
| 264 | { |
||
| 265 | return mSounds.get(soundName); |
||
| 266 | } |
||
| 267 | } |