I am trying to set up a media player with a sound which can be played and replayed as many times desired. But I am getting this error while playing the audio the first time:
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
My code that calls the play is:
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);
}
}
};
The playSong()
function is:
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());
}
}
My sound is located in res/raw/a.mp3
When I press the btnPlay
button I am displayed with the btnStop
button but nothing is playing.
I am looking to let the user play and stop and play and so forth. How can I accomplish it?
LogCat:
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