static byte[] readSoundData(String name) {
ByteArrayOutputStream out;
InputStream in;
byte[] res = null;
try {
in = name.getClass().getResourceAsStream(name);
out = new ByteArrayOutputStream();
for (int ret = in.read(); ret >= 0; ret = in.read())
out.write(ret);
res = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
static void soundLoad(int soundID) {
try {
//ByteArrayInputStream bis = null;
//DataInputStream dis = new DataInputStream(FILE_NAME_SOUNDS.getClass().getResourceAsStream(FILE_NAME_SOUNDS));
//DataInputStream dis = new DataInputStream(FILE_NAME_SOUNDS.getClass().getResourceAsStream(FILE_NAME_SOUNDS));
int sndCount = 5;
int temp;
s_soundData = new byte[sndCount][];
//sndCount = dis.read();
//s_sounds = new Player[sndCount];
//for (int i = 0; i < sndCount; i++) {
//int size = readNumber(dis, 2);
if(/*i == 0 && */soundID == 0){
temp = readSoundData("/AlexandraLedermann/sfx_good_jump.wav").length;
s_soundData[soundID] = new byte[temp];
s_soundData[soundID] = readSoundData("/AlexandraLedermann/sfx_good_jump.wav");
}
else if(/*i == 1 && */soundID == 1){ //unreached here
temp = readSoundData("/AlexandraLedermann/m_townmap.wav").length;
s_soundData[soundID] = new byte[temp];
s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_townmap.wav");
}
else if(/*i == 2 && */soundID == 2){
temp = readSoundData("/AlexandraLedermann/m_congratulation.wav").length;
s_soundData[soundID] = new byte[temp];
s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_congratulation.wav");
}
else if(/*i == 3 && */soundID == 3){ //unreached here
temp = readSoundData("/AlexandraLedermann/m_failure.wav").length;
s_soundData[soundID] = new byte[temp];
s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_failure.wav");
}
else if(/*i == 4 && */soundID == 4){
temp = readSoundData("/AlexandraLedermann/m_title.wav").length;
s_soundData[soundID] = new byte[temp];
s_soundData[soundID] = readSoundData("/AlexandraLedermann/m_title.wav");
}else{
s_soundData[soundID] = null;
}
}
catch (Exception e) {
{
e.printStackTrace();
}
}
System.gc();
}
static void soundRelease() {
if (s_currentSound != null)
{
s_currentSound.stop();
s_currentSound.release();
s_currentSound = null;
}
if (s_soundData != null)
{
for (int i = 0;i < s_soundData.length; i++ )
{
if (s_soundData[i] != null)
{
s_soundData[i] = null;
}
}
s_soundData = null;
}
isSoundLoaded = false;
System.gc();
}
static void Snd_setChannelVolume(int channel, int volume) throws Exception
{
try
{
if(s_currentSound != null)
s_currentSound.setGain(150);
}
catch (Exception e)
{
s_currentSound = null;
}
}
static void Snd_prepare(int index, int channel) throws Exception
{
try
{
if(s_currentSound == null)
s_currentSound = new Sound(s_soundData[index], Sound.FORMAT_WAV));// if wav format
//s_currentSound = new Sound(s_soundData[index], Sound.FORMAT_TONE));// if midi, ott format
}
catch (Exception e)
{
e.printStackTrace();
s_currentSound = null;
}
}
public static void soundPlay(int soundID, int loop) {
if ( (!s_soundEnable) )
{
return;
}
try {
if ( (s_currentSound != null) && (s_currentSoundId == soundID)
&& (s_currentSound.getState() == Sound.SOUND_PLAYING)
)
return;
if (s_currentSoundId > 0)
soundStop(s_currentSoundId);
if ( s_currentSound != null)
{
s_currentSound.stop();
s_currentSound.release();
s_currentSound = null;
}
s_currentSoundId = soundID;
s_isPlayingID = soundID;
if (s_soundData[soundID] == null)
{
soundLoad(soundID);
}
if ((s_currentSound != null) && (s_currentSound.getState() == Sound.SOUND_PLAYING))
return ;
// Create player
Snd_prepare(soundID, 0);
// Set volume
Snd_setChannelVolume(0, 0);
//play
s_currentSound.play(loop);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void soundStop(int id) {
try {
if (CRes.bDebugMode) {
System.out.println("soundStop : " + id);
}
if (s_currentSound == null) {
return;
}
// stop all sounds
if (id == -1) {
if (s_currentSound != null) {
s_currentSound.stop();
s_currentSound.release();
s_currentSound = null;
}
}
// stop the specific sound
else {
if (s_currentSound != null) {
s_currentSound.stop();
s_currentSound.release();
s_currentSound = null;
}
}
//s_currentSound = null;
//s_isPlayingID = -1;
System.gc();
}
catch (Exception ex) {
ex.printStackTrace();
}
}