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. 39
      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);
if (mp != null) {
mp.setVolume(src.getVolume(), src.getVolume());
} else {
soundPool.setVolume(src.getChannel(), src.getVolume(), soundPool.setVolume(src.getChannel(), src.getVolume(),
src.getVolume()); src.getVolume());
}
break; break;
case Pitch: case Pitch:
@ -228,7 +230,7 @@ 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 // Calc the distance to the listener
distanceVector.set(listenerPosition); distanceVector.set(listenerPosition);
distanceVector.subtractLocal(src.getLocalTranslation()); distanceVector.subtractLocal(src.getLocalTranslation());
@ -251,7 +253,7 @@ public class AndroidAudioRenderer implements AudioRenderer,
audioData.setCurrentVolume(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 {
if (src.getStatus() == Status.Stopped) {
mp.reset();
AssetKey<?> key = audioData.getAssetKey(); AssetKey<?> key = audioData.getAssetKey();
AssetFileDescriptor afd = assetManager.openFd(key.getName()); // assetKey.getName() AssetFileDescriptor afd = assetManager.openFd(key.getName()); // assetKey.getName()
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength()); afd.getLength());
mp.prepare(); mp.prepare();
mp.setLooping(src.isLooping()); setSourceParams(src, mp);
mp.start();
src.setChannel(0); src.setChannel(0);
src.setStatus(Status.Playing); src.setStatus(Status.Playing);
musicPlaying.put(src, mp); 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