Reputation: 94
I have a media player which plays mp3 from an URL. When I play a song and press the back button, the application gives an error. I used prepareAsync();
.
I am new, so sorry if you cannot understand me well.
Here is my play
function and ondestroy
function. Thanks.
public void playSong(final int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);
try {
mp.reset();
mp.setDataSource(naatpaths[naatindex]);
Log.w("Play Song", "CS"+naatindex);
tv.setText("Buffering Please Wait....!");
songCurrentDurationLabel.setVisibility(View.INVISIBLE);
tv.setSelected(true);
songTotalDurationLabel.setVisibility(View.INVISIBLE);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp){
songCurrentDurationLabel.setVisibility(View.VISIBLE);
songTotalDurationLabel.setVisibility(View.VISIBLE);
mp.start();
tv.setText(naattitles[naatindex]);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}
});
mp.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.w("Unable to Play", "CS"+naatindex);
playSong(currentSongIndex+1);
return false;
}
});
// Set focus to the textview
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy(){
mp.release();
mp = null;
super.onDestroy();
}
here is my log error
01-10 16:16:40.339: W/dalvikvm(26539): threadid=1: thread exiting with uncaught exception (group=0x416282a0)
01-10 16:16:40.344: E/AndroidRuntime(26539): FATAL EXCEPTION: main
01-10 16:16:40.344: E/AndroidRuntime(26539): java.lang.NullPointerException
01-10 16:16:40.344: E/AndroidRuntime(26539): at com.maulantariqjameel.bayyan.media_player$1.run(media_player.java:5032)
01-10 16:16:40.344: E/AndroidRuntime(26539): at android.os.Handler.handleCallback(Handler.java:615)
01-10 16:16:40.344: E/AndroidRuntime(26539): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 16:16:40.344: E/AndroidRuntime(26539): at android.os.Looper.loop(Looper.java:137)
01-10 16:16:40.344: E/AndroidRuntime(26539): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-10 16:16:40.344: E/AndroidRuntime(26539): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 16:16:40.344: E/AndroidRuntime(26539): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 16:16:40.344: E/AndroidRuntime(26539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-10 16:16:40.344: E/AndroidRuntime(26539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-10 16:16:40.344: E/AndroidRuntime(26539): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 3685
Reputation: 967
@Override
protected void onDestroy() {
if (runnableObject != null)
myHandler.removeCallbacks(runnableObject );
super.onDestroy();
}
private void backPressed() {
if (mpAudioSdCard != null) {
mpAudioSdCard.stop();
mpAudioSdCard.release();
mpAudioSdCard = null;
}
}
try this it will work for me i think ur mistake is ur call super.onDestroy(); first
Upvotes: 2
Reputation: 24417
Looking at the logs, it appears that the error is happening inside a Runnable
that you have posted to a Handler
. I'm guessing that you posted it using postDelayed() to update the UI or something based on the MediaPlayer
state at a regular interval. Am I right? You are setting mp to null in the onDestroy() but the Runnable
keeps running and tries to access it, causing the NullPointerException
.
To fix this, you need to clear the handler's queue of pending Runnables in your onDestroy(), like this:
mHandler.removeCallbacks(null);
Even better is to do it in your onPause() and then restart it in onResume()
Upvotes: 0
Reputation:
You should call super.onbackpressed() before release..try this code
@Override
public void onBackPressed() {
super.onBackPressed();
System.out.println("onBackPressed() called");
releaseMediaPlayer();
}
Upvotes: 0
Reputation: 6108
private void releaseMediaPlayer() {
if (mp!= null) {
if(mp.isPlaying()) {
mp.stop();
}
mp.release();
mp= null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
OR
private void releaseMediaPlayer() {
if (mp!= null) {
if(mp.isPlaying()) {
mp.stop();
}
mp.release();
mp= null;
}
}
@Override
public void onBackPressed() {
releaseMediaPlayer();
finish();
}
Upvotes: 2
Reputation: 1135
In onDestroy() there is no need to call this.finish() as the activity is already being finished. Remove that line and try again.
Upvotes: 0