我正在尝试设置一个带有声音的媒体播放器,可以根据需要多次播放和重放。但是我在第一次播放音频时遇到了这个错误:
08-23 14:48:52.613: E/MediaPlayer(24194): error (1, -2147483648)
08-23 14:48:52.613: V/Preschool Basics(24194): Prepare failed.: status=0x1
我调用该剧的代码是:
btnPlay = (Button) findViewById(R.id.soundButton);
btnStop = (Button) findViewById(R.id.stopButton);
mpSound = MediaPlayer.create(this, R.raw.a);
mpSound.setLooping(false);
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//mpSound.start();
Uri uri = Uri.parse("android.resource://com.test.testing/" + R.raw.a);
//Toast.makeText(getApplicationContext(), uri.toString(), 2000).show();
playSong(uri.toString());
btnPlay.setVisibility(View.GONE);
btnStop.setVisibility(View.VISIBLE);
btnStop.setOnClickListener(stopSound);
}
});
View.OnClickListener stopSound = new View.OnClickListener() {
public void onClick(View v) {
if (mpSound != null) {
mpSound.stop();
//mpSound.release();
btnPlay.setVisibility(View.VISIBLE);
btnStop.setVisibility(View.GONE);
}
}
};
playSong()
功能是:
private void playSong(String songPath) {
try {
mpSound.reset();
mpSound.setDataSource(songPath);
mpSound.prepare();
mpSound.start();
//Stop the song and replace button
mpSound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mpSound.stop();
btnPlay.setVisibility(View.VISIBLE);
btnStop.setVisibility(View.GONE);
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
我的声音位于res/raw/a.mp3
当我按下btnPlay
按钮时,我会显示btnStop
按钮,但没有播放任何内容。
我希望让用户播放和停止播放等等。我怎样才能实现它?
日志猫:
08-23 15:41:20.761: E/AndroidRuntime(29438): FATAL EXCEPTION: main
08-23 15:41:20.761: E/AndroidRuntime(29438): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.testing/com.test.testing.AlpDisplay}: java.lang.NullPointerException
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread.access$600(ActivityThread.java:150)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.os.Looper.loop(Looper.java:137)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread.main(ActivityThread.java:5195)
08-23 15:41:20.761: E/AndroidRuntime(29438): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 15:41:20.761: E/AndroidRuntime(29438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-23 15:41:20.761: E/AndroidRuntime(29438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-23 15:41:20.761: E/AndroidRuntime(29438): at dalvik.system.NativeStart.main(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438): Caused by: java.lang.NullPointerException
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.media.MediaPlayer.create(MediaPlayer.java:824)
08-23 15:41:20.761: E/AndroidRuntime(29438): at com.test.testing.MusicPlayer.<init>(MusicPlayer.java:13)
08-23 15:41:20.761: E/AndroidRuntime(29438): at com.test.testing.AlpDisplay.<init>(AlpDisplay.java:45)
08-23 15:41:20.761: E/AndroidRuntime(29438): at java.lang.Class.newInstanceImpl(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438): at java.lang.Class.newInstance(Class.java:1319)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-23 15:41:20.761: E/AndroidRuntime(29438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
08-23 15:41:20.761: E/AndroidRuntime(29438): ... 11 more