Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK;

import com.gebauz.pingK.gamestates.GameStateManager;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class Sounds
{
    private final int[] mResourceIds;
    private final int[] mSoundPoolIds;

    private SoundPool mSoundPool;
    private AudioManager mAudioManager;
    private boolean mSoundsOn;

    public Sounds(int[] soundResourceIds)
    {
        this.mResourceIds = soundResourceIds;
        mSoundPoolIds = new int[soundResourceIds.length];
        mSoundsOn = true;
    }

    public void init(Context context)
    {
        mSoundPool = new SoundPool(200, AudioManager.STREAM_MUSIC, 100);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        for (int i = 0; i < mResourceIds.length; i++)
        {
            mSoundPoolIds[i] = mSoundPool.load(context, mResourceIds[i], 100);
        }
    }

    public void exit()
    {
        mAudioManager = null;
        mSoundPool.release();
        mSoundPool = null;
    }

    public void playSound(int sound)
    {
        if (mSoundPool != null && mSoundsOn)
        {
            for (int i = 0; i < mResourceIds.length; i++)
            {
                if (sound == mResourceIds[i])
                {
                    int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                    mSoundPool.play(mSoundPoolIds[i], streamVolume, streamVolume, 1, 0, 1f);
                    break;
                }
            }
        }
    }

    public boolean isSoundsOn()
    {
        return mSoundsOn;
    }

    public void setSoundsOn(boolean soundsOn)
    {
        this.mSoundsOn = soundsOn;
    }

}