AndroidAudioRenderer : better mediaplayer life cycle handling

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9296 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 13 years ago
parent 544e1f6342
commit 2966b6a7b0
  1. 95
      engine/src/android/com/jme3/audio/android/AndroidAudioRenderer.java

@ -38,7 +38,6 @@ import android.content.res.AssetManager;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.media.SoundPool; import android.media.SoundPool;
import android.util.Log;
import com.jme3.asset.AssetKey; import com.jme3.asset.AssetKey;
import com.jme3.audio.AudioNode.Status; import com.jme3.audio.AudioNode.Status;
@ -47,7 +46,6 @@ import com.jme3.math.FastMath;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -179,9 +177,13 @@ public class AndroidAudioRenderer implements AudioRenderer,
} }
break; break;
case Volume: case Volume:
MediaPlayer mp = musicPlaying.get(src);
soundPool.setVolume(src.getChannel(), src.getVolume(), if (mp != null) {
src.getVolume()); mp.setVolume(src.getVolume(), src.getVolume());
} else {
soundPool.setVolume(src.getChannel(), src.getVolume(),
src.getVolume());
}
break; break;
case Pitch: case Pitch:
@ -228,30 +230,30 @@ public class AndroidAudioRenderer implements AudioRenderer,
for (AudioNode src : musicPlaying.keySet()) { for (AudioNode src : musicPlaying.keySet()) {
MediaPlayer mp = musicPlaying.get(src); MediaPlayer mp = musicPlaying.get(src);
{
// Calc the distance to the listener
distanceVector.set(listenerPosition);
distanceVector.subtractLocal(src.getLocalTranslation());
distance = FastMath.abs(distanceVector.length());
if (distance < src.getRefDistance()) { // Calc the distance to the listener
distance = src.getRefDistance(); distanceVector.set(listenerPosition);
} distanceVector.subtractLocal(src.getLocalTranslation());
if (distance > src.getMaxDistance()) { distance = FastMath.abs(distanceVector.length());
distance = src.getMaxDistance();
}
volume = src.getRefDistance() / distance;
AndroidAudioData audioData = (AndroidAudioData) src.getAudioData(); if (distance < src.getRefDistance()) {
distance = src.getRefDistance();
}
if (distance > src.getMaxDistance()) {
distance = src.getMaxDistance();
}
volume = src.getRefDistance() / distance;
if (FastMath.abs(audioData.getCurrentVolume() - volume) > FastMath.FLT_EPSILON) { AndroidAudioData audioData = (AndroidAudioData) src.getAudioData();
// Left / Right channel get the same volume by now, only
// positional
mp.setVolume(volume, volume);
audioData.setCurrentVolume(volume); if (FastMath.abs(audioData.getCurrentVolume() - volume) > FastMath.FLT_EPSILON) {
} // Left / Right channel get the same volume by now, only
// positional
mp.setVolume(volume, volume);
audioData.setCurrentVolume(volume);
} }
} }
} }
@ -341,6 +343,7 @@ public class AndroidAudioRenderer implements AudioRenderer,
soundpoolStillLoading.put(audioData.getId(), src); soundpoolStillLoading.put(audioData.getId(), src);
} else { } else {
src.setChannel(channel); // receive a channel at the last src.setChannel(channel); // receive a channel at the last
setSourceParams(src);
// playing at least // playing at least
} }
} catch (IOException e) { } catch (IOException e) {
@ -362,11 +365,12 @@ public class AndroidAudioRenderer implements AudioRenderer,
AudioData audioData = src.getAudioData(); AudioData audioData = src.getAudioData();
if (status == 0) // load was successfull // load was successfull
{ if (status == 0) {
int channelIndex; int channelIndex;
channelIndex = soundPool.play(audioData.getId(), 1f, 1f, 1, 0, 1f); channelIndex = soundPool.play(audioData.getId(), 1f, 1f, 1, 0, 1f);
src.setChannel(channelIndex); src.setChannel(channelIndex);
setSourceParams(src);
} }
} }
@ -385,18 +389,22 @@ public class AndroidAudioRenderer implements AudioRenderer,
} }
try { try {
AssetKey<?> key = audioData.getAssetKey(); if (src.getStatus() == Status.Stopped) {
mp.reset();
AssetFileDescriptor afd = assetManager.openFd(key.getName()); // assetKey.getName() AssetKey<?> key = audioData.getAssetKey();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength()); AssetFileDescriptor afd = assetManager.openFd(key.getName()); // assetKey.getName()
mp.prepare(); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
mp.setLooping(src.isLooping()); afd.getLength());
mp.start(); mp.prepare();
src.setChannel(0); setSourceParams(src, mp);
src.setStatus(Status.Playing); src.setChannel(0);
musicPlaying.put(src, mp); src.setStatus(Status.Playing);
musicPlaying.put(src, mp);
mp.start();
}else{
mp.start();
}
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
e.printStackTrace(); e.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
@ -404,6 +412,17 @@ public class AndroidAudioRenderer implements AudioRenderer,
} }
} }
private void setSourceParams(AudioNode src, MediaPlayer mp) {
mp.setLooping(true);
mp.setVolume(src.getVolume(), src.getVolume());
//src.getDryFilter();
}
private void setSourceParams(AudioNode src) {
soundPool.setLoop(src.getChannel(), src.isLooping() ? -1 : 0);
soundPool.setVolume(src.getChannel(), src.getVolume(), src.getVolume());
}
/** /**
* Pause the current playing sounds. Both from the {@link SoundPool} and the * Pause the current playing sounds. Both from the {@link SoundPool} and the
* active {@link MediaPlayer}s * active {@link MediaPlayer}s

Loading…
Cancel
Save