我有一个Android应用程序,从服务器下载声音和图像,以进行听力测试。 但是,我对SoundPool有以下问题:
我已经把它恢复到了骨头,下面的代码复制了我的问题:
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);
}
}
}
这是LogCat输出,它的价值在哪里:
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
错误似乎发生在第45行的负载上:
int loaded = soundPool.load(soundPath + soundWords[i], 1);
下载和保存似乎正常工作,实际上对于此示例,在运行上述示例之前,文件位于文件夹中。
如果有人能够看到这里发生了什么并提出任何建议,我们将不胜感激。
答案 0 :(得分:3)
您无法在任何地方实例化soundPool
对象,这就是您获取原因的原因:
Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
你应该使用类似的东西:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
...在加载样品之前。
来自Android开发者网站的SoundPool的更多信息。虽然经过仔细检查,使用SoundPool
的首选方法现在使用了构建器,因为现在不推荐使用某些方法:SoundPool.Builder