package com.gebauz.pingK.common.game;
import com.gebauz.pingK.common.R;
import com.gebauz.pingK.common.framework.ResourceManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.util.Log;
public class Sounds
{
private int[] mResourceIds =
null;
private int[] mSoundPoolIds =
null;
private SoundPool mSoundPool
;
private AudioManager mAudioManager
;
private boolean mSoundsOn
;
private MediaPlayer mMusic =
null;
private boolean mMusicEnabled =
true;
private int mMusicId = -
1;
public Sounds
()
{
SharedPreferences myPrefs = ResourceManager.
getInstance().
getCurrentContext().
getSharedPreferences(GameConsts.
PREF_NAME,
Context.
MODE_PRIVATE);
boolean musicEnabled = myPrefs.
getBoolean(GameConsts.
PREF_MUSIC_ENABLED,
true);
enableMusic
(musicEnabled
);
}
public void init
(int[] soundResourceIds
)
{
this.
mResourceIds = soundResourceIds
;
mSoundPoolIds =
new int[soundResourceIds.
length];
mSoundsOn =
true;
Context context = ResourceManager.
getInstance().
getCurrentContext();
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
()
{
this.
mResourceIds =
null;
mSoundPoolIds =
null;
mAudioManager =
null;
if (mSoundPool
!=
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
;
this.
pauseMusic(!mSoundsOn
);
}
public void playMusic
(int id
)
{
Log.
v(GameConsts.
LOG_TAG,
"Play Music");
stopMusic
();
if (id == -
1)
return;
mMusic = MediaPlayer.
create(ResourceManager.
getInstance().
getCurrentContext(), id
);
mMusic.
seekTo(0);
mMusicId = id
;
if (mSoundsOn
&& mMusicEnabled
)
{
mMusic.
start();
}
}
public void pauseMusic
(boolean pause
)
{
if (mMusic
!=
null)
{
if (pause
)
{
if (mMusic.
isPlaying())
{
mMusic.
pause();
Log.
v(GameConsts.
LOG_TAG,
"Pause Music");
}
}
else
{
if (mSoundsOn
)
{
mMusic.
start();
Log.
v(GameConsts.
LOG_TAG,
"Resume Music");
}
}
}
}
public void stopMusic
()
{
if (mMusic
!=
null)
{
mMusic.
stop();
mMusic.
release();
mMusic =
null;
}
}
public void onPause
()
{
if (mMusic
!=
null)
mMusic.
pause();
}
public void onResume
()
{
if (mMusic
!=
null)
mMusic.
start();
}
public void enableMusic
(boolean enable
)
{
mMusicEnabled = enable
;
// store in prefs
SharedPreferences myPrefs = ResourceManager.
getInstance().
getCurrentContext().
getSharedPreferences(GameConsts.
PREF_NAME,
Context.
MODE_PRIVATE);
SharedPreferences.
Editor prefsEditor = myPrefs.
edit();
//prefsEditor.putBoolean(GameConsts.PREF_AUDIO_ENABLED, enable);
prefsEditor.
putBoolean(GameConsts.
PREF_MUSIC_ENABLED, enable
);
prefsEditor.
commit();
if (mMusicEnabled
)
{
playMusic
(mMusicId
);
}
else
{
stopMusic
();
}
}
public boolean isMusicEnabled
()
{
return mMusicEnabled
;
}
}