Fixed NPE in AndroidAudioRenderer when soundPool is not initialized

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9175 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 13 years ago
parent c4231e3200
commit 446c03935e
  1. 19
      engine/src/android/com/jme3/audio/android/AndroidAudioRenderer.java

@ -60,12 +60,10 @@ import java.util.logging.Logger;
public class AndroidAudioRenderer implements AudioRenderer, public class AndroidAudioRenderer implements AudioRenderer,
SoundPool.OnLoadCompleteListener, MediaPlayer.OnCompletionListener { SoundPool.OnLoadCompleteListener, MediaPlayer.OnCompletionListener {
private static final Logger logger = Logger private static final Logger logger = Logger.getLogger(AndroidAudioRenderer.class.getName());
.getLogger(AndroidAudioRenderer.class.getName());
private final static int MAX_NUM_CHANNELS = 16; private final static int MAX_NUM_CHANNELS = 16;
private final HashMap<AudioNode, MediaPlayer> musicPlaying = new HashMap<AudioNode, MediaPlayer>(); private final HashMap<AudioNode, MediaPlayer> musicPlaying = new HashMap<AudioNode, MediaPlayer>();
private SoundPool soundPool = null; private SoundPool soundPool = null;
private final Vector3f listenerPosition = new Vector3f(); private final Vector3f listenerPosition = new Vector3f();
// For temp use // For temp use
private final Vector3f distanceVector = new Vector3f(); private final Vector3f distanceVector = new Vector3f();
@ -74,13 +72,11 @@ public class AndroidAudioRenderer implements AudioRenderer,
private HashMap<Integer, AudioNode> soundpoolStillLoading = new HashMap<Integer, AudioNode>(); private HashMap<Integer, AudioNode> soundpoolStillLoading = new HashMap<Integer, AudioNode>();
private Listener listener; private Listener listener;
private boolean audioDisabled = false; private boolean audioDisabled = false;
private final AudioManager manager; private final AudioManager manager;
public AndroidAudioRenderer(Activity context) { public AndroidAudioRenderer(Activity context) {
this.context = context; this.context = context;
manager = (AudioManager) context manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
.getSystemService(Context.AUDIO_SERVICE);
context.setVolumeControlStream(AudioManager.STREAM_MUSIC); context.setVolumeControlStream(AudioManager.STREAM_MUSIC);
assetManager = context.getAssets(); assetManager = context.getAssets();
} }
@ -246,8 +242,7 @@ public class AndroidAudioRenderer implements AudioRenderer,
} }
volume = src.getRefDistance() / distance; volume = src.getRefDistance() / distance;
AndroidAudioData audioData = (AndroidAudioData) src AndroidAudioData audioData = (AndroidAudioData) src.getAudioData();
.getAudioData();
if (FastMath.abs(audioData.getCurrentVolume() - volume) > FastMath.FLT_EPSILON) { if (FastMath.abs(audioData.getCurrentVolume() - volume) > FastMath.FLT_EPSILON) {
// Left / Right channel get the same volume by now, only // Left / Right channel get the same volume by now, only
@ -413,21 +408,25 @@ public class AndroidAudioRenderer implements AudioRenderer,
* active {@link MediaPlayer}s * active {@link MediaPlayer}s
*/ */
public void pauseAll() { public void pauseAll() {
if (soundPool != null) {
soundPool.autoPause(); soundPool.autoPause();
for (MediaPlayer mp : musicPlaying.values()) { for (MediaPlayer mp : musicPlaying.values()) {
mp.pause(); mp.pause();
} }
} }
}
/** /**
* Resume all paused sounds. * Resume all paused sounds.
*/ */
public void resumeAll() { public void resumeAll() {
if (soundPool != null) {
soundPool.autoResume(); soundPool.autoResume();
for (MediaPlayer mp : musicPlaying.values()) { for (MediaPlayer mp : musicPlaying.values()) {
mp.start(); //no resume -> api says call start to resume mp.start(); //no resume -> api says call start to resume
} }
} }
}
public void pauseSource(AudioNode src) { public void pauseSource(AudioNode src) {
if (audioDisabled) { if (audioDisabled) {
@ -440,9 +439,9 @@ public class AndroidAudioRenderer implements AudioRenderer,
src.setStatus(Status.Paused); src.setStatus(Status.Paused);
} else { } else {
int channel = src.getChannel(); int channel = src.getChannel();
if (channel != -1) if (channel != -1) {
soundPool.pause(channel); // is not very likley to make soundPool.pause(channel); // is not very likley to make
// something useful :) } // something useful :)
} }
} }

Loading…
Cancel
Save