Subversion Repositories AndroidProjects

Rev

Rev 273 | Rev 324 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.audio;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.app.GameObject;

public class Audio extends GameObject
{
    private static final String PREF_MUSIC_ENABLED = "music.enabled";
    private static final String PREF_SOUND_ENABLED = "sound.enabled";
       
        private boolean mSoundEnabled = true;
        private boolean mMusicEnabled = true;
       
        private Vector<Sound> mSounds = new Vector<Sound>();
       
        public Audio(Game game)
        {
                super(game);           
        }
       
        public void init()
        {
                mSoundEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_SOUND_ENABLED, true);
                mMusicEnabled = Gdx.app.getPreferences(getGame().getGameTitle()).getBoolean(PREF_MUSIC_ENABLED, true);
        }
       
        public void exit()
        {
                clearSounds();
        }
       
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
               
        }
       
        public void onPause()
        {
               
        }
       
        public void onResume()
        {
               
        }
       
        public void clearSounds()
        {
                for (int i = 0; i < mSounds.size(); i++)
                {
                        mSounds.get(i).dispose();
                }
                mSounds.clear();
        }
       
        public Sound newManagedSound(String filename)
        {
                Sound sound = Gdx.audio.newSound(Gdx.files.internal(filename));
               
                mSounds.add(sound);
               
                return sound;
        }
       
        public void removeManagedSound(Sound sound)
        {
                if (sound == null)
                        return;
               
                int i = mSounds.indexOf(sound);
                if (i != -1)
                {                      
                        mSounds.remove(i);
                }
               
                // regardless of whether in list, dispose
                sound.dispose();
        }
       
        public void playSound(Sound sound)
        {
                if (!isSoundEnabled())
                        return;
               
                sound.play();
        }
       
        public void playSound(Sound sound, float volume)
        {
                if (!isSoundEnabled())
                        return;
               
                sound.play(volume);
        }
       
       
        public void playSound(Sound sound, float volume, float pitch, float pan)
        {
                if (!isSoundEnabled())
                        return;
               
                sound.play(volume, pitch, pan);
        }
       
        public final boolean isSoundEnabled()
        {
                return mSoundEnabled;
        }
       
        public final boolean isMusicEnabled()
        {
                return mMusicEnabled;
        }
       
        public final void setSoundEnabled(boolean enabled)
        {
                mSoundEnabled = enabled;
               
                // stop sounds playing
                for (int i = 0; i < mSounds.size(); i++)
                {
                        mSounds.get(i).stop();
                }
               
                Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled);
                Gdx.app.getPreferences(getGame().getGameTitle()).flush();
        }
       
        public final void setMusicEnabled(boolean enabled)
        {
                mMusicEnabled = enabled;
               
                Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled);
                Gdx.app.getPreferences(getGame().getGameTitle()).flush();
        }
       
}