Rev 361 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 273 | chris | 1 | package com.gebauz.Bauzoid.audio; |
| 2 | |||
| 319 | chris | 3 | import java.util.Vector; |
| 4 | |||
| 5 | import com.badlogic.gdx.Gdx; |
||
| 324 | chris | 6 | import com.badlogic.gdx.audio.Music; |
| 319 | chris | 7 | import com.badlogic.gdx.audio.Sound; |
| 273 | chris | 8 | import com.gebauz.Bauzoid.app.Game; |
| 9 | import com.gebauz.Bauzoid.app.GameObject; |
||
| 10 | |||
| 11 | public class Audio extends GameObject |
||
| 12 | { |
||
| 319 | chris | 13 | private static final String PREF_MUSIC_ENABLED = "music.enabled"; |
| 14 | private static final String PREF_SOUND_ENABLED = "sound.enabled"; |
||
| 273 | chris | 15 | |
| 319 | chris | 16 | private boolean mSoundEnabled = true; |
| 17 | private boolean mMusicEnabled = true; |
||
| 362 | chris | 18 | private boolean mMusicLooping = false; |
| 319 | chris | 19 | |
| 20 | private Vector<Sound> mSounds = new Vector<Sound>(); |
||
| 324 | chris | 21 | private Music mMusic = null; |
| 319 | chris | 22 | |
| 273 | chris | 23 | public Audio(Game game) |
| 24 | { |
||
| 25 | super(game); |
||
| 26 | } |
||
| 27 | |||
| 28 | public void init() |
||
| 29 | { |
||
| 319 | chris | 30 | mSoundEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_SOUND_ENABLED, true); |
| 31 | mMusicEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_MUSIC_ENABLED, true); |
||
| 273 | chris | 32 | } |
| 33 | |||
| 34 | public void exit() |
||
| 35 | { |
||
| 319 | chris | 36 | clearSounds(); |
| 324 | chris | 37 | if (mMusic != null) |
| 38 | { |
||
| 39 | mMusic.dispose(); |
||
| 40 | mMusic = null; |
||
| 41 | } |
||
| 273 | chris | 42 | } |
| 43 | |||
| 44 | public void update(float deltaTime) |
||
| 45 | { |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | public void render() |
||
| 50 | { |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 54 | public void onPause() |
||
| 55 | { |
||
| 56 | |||
| 57 | } |
||
| 58 | |||
| 59 | public void onResume() |
||
| 60 | { |
||
| 61 | |||
| 62 | } |
||
| 319 | chris | 63 | |
| 64 | public void clearSounds() |
||
| 65 | { |
||
| 66 | for (int i = 0; i < mSounds.size(); i++) |
||
| 67 | { |
||
| 68 | mSounds.get(i).dispose(); |
||
| 69 | } |
||
| 70 | mSounds.clear(); |
||
| 71 | } |
||
| 72 | |||
| 73 | public Sound newManagedSound(String filename) |
||
| 74 | { |
||
| 75 | Sound sound = Gdx.audio.newSound(Gdx.files.internal(filename)); |
||
| 76 | |||
| 77 | mSounds.add(sound); |
||
| 78 | |||
| 79 | return sound; |
||
| 80 | } |
||
| 81 | |||
| 82 | public void removeManagedSound(Sound sound) |
||
| 83 | { |
||
| 84 | if (sound == null) |
||
| 85 | return; |
||
| 86 | |||
| 87 | int i = mSounds.indexOf(sound); |
||
| 88 | if (i != -1) |
||
| 89 | { |
||
| 90 | mSounds.remove(i); |
||
| 91 | } |
||
| 92 | |||
| 93 | // regardless of whether in list, dispose |
||
| 94 | sound.dispose(); |
||
| 95 | } |
||
| 96 | |||
| 324 | chris | 97 | public void loadMusic(String filename) |
| 98 | { |
||
| 99 | if (mMusic != null) |
||
| 100 | { |
||
| 101 | mMusic.dispose(); |
||
| 102 | mMusic = null; |
||
| 103 | } |
||
| 104 | mMusic = Gdx.audio.newMusic(Gdx.files.internal(filename)); |
||
| 105 | } |
||
| 106 | |||
| 362 | chris | 107 | public void playMusic(String filename, boolean looping) |
| 328 | chris | 108 | { |
| 109 | loadMusic(filename); |
||
| 362 | chris | 110 | playMusic(looping); |
| 328 | chris | 111 | } |
| 112 | |||
| 362 | chris | 113 | public void playMusic(boolean looping) |
| 324 | chris | 114 | { |
| 115 | if (isMusicEnabled() && (mMusic != null)) |
||
| 116 | { |
||
| 362 | chris | 117 | mMusicLooping = looping; |
| 118 | mMusic.setLooping(looping); |
||
| 324 | chris | 119 | mMusic.play(); |
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public void pauseMusic() |
||
| 124 | { |
||
| 125 | if (mMusic != null) |
||
| 126 | { |
||
| 328 | chris | 127 | mMusic.pause(); |
| 324 | chris | 128 | } |
| 129 | } |
||
| 130 | |||
| 361 | chris | 131 | public boolean isMusicPlaying() |
| 132 | { |
||
| 133 | if (mMusic == null) |
||
| 134 | return false; |
||
| 135 | |||
| 136 | return mMusic.isPlaying(); |
||
| 137 | } |
||
| 138 | |||
| 319 | chris | 139 | public void playSound(Sound sound) |
| 140 | { |
||
| 141 | if (!isSoundEnabled()) |
||
| 142 | return; |
||
| 143 | |||
| 144 | sound.play(); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void playSound(Sound sound, float volume) |
||
| 148 | { |
||
| 149 | if (!isSoundEnabled()) |
||
| 150 | return; |
||
| 151 | |||
| 152 | sound.play(volume); |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | public void playSound(Sound sound, float volume, float pitch, float pan) |
||
| 157 | { |
||
| 158 | if (!isSoundEnabled()) |
||
| 159 | return; |
||
| 160 | |||
| 161 | sound.play(volume, pitch, pan); |
||
| 162 | } |
||
| 163 | |||
| 164 | public final boolean isSoundEnabled() |
||
| 165 | { |
||
| 166 | return mSoundEnabled; |
||
| 167 | } |
||
| 168 | |||
| 169 | public final boolean isMusicEnabled() |
||
| 170 | { |
||
| 171 | return mMusicEnabled; |
||
| 172 | } |
||
| 173 | |||
| 174 | public final void setSoundEnabled(boolean enabled) |
||
| 175 | { |
||
| 176 | mSoundEnabled = enabled; |
||
| 177 | |||
| 178 | // stop sounds playing |
||
| 324 | chris | 179 | if (!isSoundEnabled()) |
| 319 | chris | 180 | { |
| 324 | chris | 181 | for (int i = 0; i < mSounds.size(); i++) |
| 182 | { |
||
| 183 | mSounds.get(i).stop(); |
||
| 184 | } |
||
| 319 | chris | 185 | } |
| 186 | |||
| 187 | Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled); |
||
| 188 | Gdx.app.getPreferences(getGame().getGameTitle()).flush(); |
||
| 189 | } |
||
| 190 | |||
| 191 | public final void setMusicEnabled(boolean enabled) |
||
| 192 | { |
||
| 193 | mMusicEnabled = enabled; |
||
| 194 | |||
| 324 | chris | 195 | if (isMusicEnabled()) |
| 196 | { |
||
| 362 | chris | 197 | playMusic(mMusicLooping); |
| 324 | chris | 198 | } |
| 199 | else |
||
| 200 | { |
||
| 201 | pauseMusic(); |
||
| 202 | } |
||
| 203 | |||
| 319 | chris | 204 | Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled); |
| 324 | chris | 205 | Gdx.app.getPreferences(getGame().getGameTitle()).flush(); |
| 319 | chris | 206 | } |
| 207 | |||
| 273 | chris | 208 | } |