Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.audio;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.gebauz.bauzoid.file.FileUtil;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.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 boolean mMusicLooping = false;
       
        //private Vector<Sound> mSounds = new Vector<Sound>();
        private HashMap<String, Sound> mSounds = new HashMap<String, Sound>();
        private Music mMusic = null;
       
        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();
                if (mMusic != null)
                {
                        mMusic.dispose();
                        mMusic = null;
                }
        }
       
        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();
                }*/

               
                for (Map.Entry<String, Sound> entry : mSounds.entrySet())
                {
                    entry.getValue().dispose();
                }                              
                mSounds.clear();
        }
       
        public Sound newManagedSound(String filename)
        {              
                String soundName = FileUtil.extractFilenameNoExt(filename);
               
                if (mSounds.containsKey(soundName))
                        return mSounds.get(soundName);
               
                Sound sound = Gdx.audio.newSound(Gdx.files.internal(filename));
                //mSounds.add(sound);
                mSounds.put(soundName, sound);
               
                return sound;
        }
       
        public void removeManagedSound(Sound sound)
        {
                if (sound == null)
                        return;
               
                while (mSounds.values().remove(sound));
               
                /*int i = mSounds.indexOf(sound;
                if (i != -1)
                {                      
                        mSounds.remove(i);
                }*/

               
                // regardless of whether in list, dispose
                sound.dispose();
        }
       
        public void loadMusic(String filename)
        {
                if (mMusic != null)
                {
                        mMusic.stop();
                        mMusic.dispose();
                        mMusic = null;                         
                }
                mMusic = Gdx.audio.newMusic(Gdx.files.internal(filename));
        }
       
        public void disposeMusic()
        {
                if (mMusic != null)
                {
                        mMusic.stop();
                        mMusic.dispose();
                        mMusic = null;
                }
        }
       
        public void playMusic(String filename, boolean looping, float volume)
        {
                loadMusic(filename);
                setMusicVolume(volume);
                playMusic(looping);
        }
       
        public void playMusic(String filename, boolean looping)
        {
                loadMusic(filename);
                playMusic(looping);
        }
       
        public void playMusic(boolean looping)
        {
                if (isMusicEnabled() && (mMusic != null))
                {
                        mMusicLooping = looping;
                        mMusic.setLooping(looping);
                        mMusic.play();
                }
        }
       
        public void pauseMusic()
        {
                if (mMusic != null)
                {
                        mMusic.pause();
                }
        }
       
        public void stopMusic()
        {
                if (mMusic != null)
                {
                        mMusic.stop();
                }
        }
       
        public boolean isMusicPlaying()
        {
                if (mMusic == null)
                        return false;
               
                return mMusic.isPlaying();
        }
       
        public void setMusicVolume(float volume)
        {
                if (mMusic != null)
                {
                        mMusic.setVolume(volume);
                }
        }
       
        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
                if (!isSoundEnabled())
                {
                        /*for (int i = 0; i < mSounds.size(); i++)
                        {
                                mSounds.get(i).stop();
                        }*/

                       
                        for (Map.Entry<String, Sound> entry : mSounds.entrySet())
                        {
                                entry.getValue().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;
               
                if (isMusicEnabled())
                {
                        playMusic(mMusicLooping);
                }
                else
                {
                        pauseMusic();
                }
               
                Gdx.app.getPreferences(getGame().getGameTitle()).putBoolean(PREF_SOUND_ENABLED, mSoundEnabled);
                Gdx.app.getPreferences(getGame().getGameTitle()).flush();              
        }
       
       
        public Sound getSound(String soundName)
        {
                return mSounds.get(soundName);
        }
}