I have an Android app that downloads sounds and images from a server to make a listening test. However, I have the following problem with a SoundPool:
I've pared it back to the bare bones, and the code below replicates my problem:
package com.grooble.moeigo;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.View;
import android.view.View.OnClickListener;
public class TestActivity extends Activity {
private String soundPath = "/storage/emulated/0/Music/questionSounds/";
private String[] soundWords = {"caravan.ogg", "clap.ogg", "cut.ogg", "hot.ogg", "knee.ogg"};
// private variables for SoundPool
private SoundPool soundPool;
SparseIntArray mySoundPoolMap = new SparseIntArray(5);
AudioManager audioManager;
boolean plays = false, loaded = false;
float actVolume, maxVolume, volume;
int counter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actVolume / maxVolume;
//Hardware buttons setting to adjust the media sound
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
doSoundPool();
}
void doSoundPool(){
View soundButton = (View) findViewById(R.id.soundButton);
for (int i=0; i<soundWords.length; i++){
int loaded = soundPool.load(soundPath + soundWords[i], 1);
mySoundPoolMap.put(i, loaded);
}
//Set clickListeners for sounds to soundButton
soundButton.setOnClickListener(new MyOnClickListener(0){
});
}
class MyOnClickListener implements OnClickListener{
private int index;
public MyOnClickListener(int index){
super();
this.index = index;
}
public void onClick(View v) {
soundPool.play((int)mySoundPoolMap.get(index), 1.0f, 1.0f, 1, 0, 1.0f);
}
}
}
here is the LogCat output, for what it's worth:
12-22 23:46:25.544: W/UserFrag(6865): onCreateView: view not null
12-22 23:46:25.547: W/ListFrag(6865): onCreateView: view not null
12-22 23:46:25.554: D/OpenGLRenderer(6865): Render dirty regions requested: true
12-22 23:46:25.561: D/Atlas(6865): Validating map...
12-22 23:46:25.587: I/Adreno-EGL(6865): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
12-22 23:46:25.588: I/OpenGLRenderer(6865): Initialized EGL, version 1.4
12-22 23:46:25.605: D/OpenGLRenderer(6865): Enabling debug mode 0
12-22 23:46:34.321: D/AndroidRuntime(6865): Shutting down VM
12-22 23:46:34.322: E/AndroidRuntime(6865): FATAL EXCEPTION: main
12-22 23:46:34.322: E/AndroidRuntime(6865): Process: com.grooble.moeigo, PID: 6865
12-22 23:46:34.322: E/AndroidRuntime(6865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grooble.moeigo/com.grooble.moeigo.TestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.os.Looper.loop(Looper.java:135)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-22 23:46:34.322: E/AndroidRuntime(6865): at java.lang.reflect.Method.invoke(Native Method)
12-22 23:46:34.322: E/AndroidRuntime(6865): at java.lang.reflect.Method.invoke(Method.java:372)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-22 23:46:34.322: E/AndroidRuntime(6865): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.grooble.moeigo.TestActivity.doSoundPool(TestActivity.java:48)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.grooble.moeigo.TestActivity.onCreate(TestActivity.java:40)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.Activity.performCreate(Activity.java:5933)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-22 23:46:34.322: E/AndroidRuntime(6865): ... 10 more
12-22 23:46:36.064: I/Process(6865): Sending signal. PID: 6865 SIG: 9
The error seems to be occurring on the load on line 45:
int loaded = soundPool.load(soundPath + soundWords[i], 1);
The download and save appears to be working and indeed for this example, the files are in the folder before the above example is run.
If anyone can see what's is going on here and offer any suggestions, it would be much appreciated.
You don't instantiate the soundPool
object anywhere, that's why you're getting:
Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
You should use something like:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
...prior to loading samples.
More info on SoundPool from the Android Developer site. Although upon closer inspection, the preferred method of using SoundPool
is now with a Builder, as certain methods are now deprecated: SoundPool.Builder