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)
#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
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();
}
In onDestroy() there is no need to call this.finish() as the activity is already being finished. Remove that line and try again.
You should call super.onbackpressed() before release..try this code
#Override
public void onBackPressed() {
super.onBackPressed();
System.out.println("onBackPressed() called");
releaseMediaPlayer();
}
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()
Related
Following problem: if I press the power button after pressing a take picture button just before the getPictureCallback() methode is called the app crashes.
The camera is loaded in a thread like this:
private class CameraHandlerThread extends HandlerThread {
Handler mHandler = null;
CameraHandlerThread() {
super("CameraHandlerThread");
start();
mHandler = new Handler(getLooper());
}
synchronized void notifyCameraOpened() {
notify();
runOnUiThread(new Runnable() {
public void run() {
setCameraParameter();
}
});
}
void openCamera() {
mHandler.post(new Runnable() {
#Override
public void run() {
openAndroidCamera();
notifyCameraOpened();
}
});
try {
wait();
}
catch (InterruptedException ex) {
LogManager.e(TAG, ex.getMessage(), ex);
CoreToastDialog.showErrorMessage(getString(R.string.FailedToLoadCamera));
}
}
}
I interrupt the thread in onPause. Any ideas how to fix this? The bug only occures in a very rare time shift - so only every 30 tries it works maybe once.
If it is PictureCallback, is there any way to fix this?
Error Message:
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x410a32a0)
W/System.err﹕ java.lang.RuntimeException: Unable to start activity ComponentInfo{DeviceListView}: java.lang.NullPointerException
W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
W/System.err﹕ at android.app.ActivityThread.access$700(ActivityThread.java:140)
W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4921)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
I know this has been posted multiple times now, but nothing seemed to fix my problem. I create a Loading Dialog on the UI thread, then start another thread which is doing some networking stuff and via interface, the class gets a callback, when networking is done. The problem is, after its done, i cant remove the loading dialog, even with "runOnUIThread". Here is the code:
public void onClickLoginButton (View view) {
if (!((EditText)findViewById(R.id.textNickname)).getText().toString().isEmpty() &&
!((EditText)findViewById(R.id.textPassword)).getText().toString().isEmpty()) {
loggingIn = new ProgressDialog(this);
loggingIn.setTitle("Login");
loggingIn.setMessage("Wait while logging in...");
loggingIn.show();
final LoginInterface callbackInterface = this;
new Thread(new Runnable() {
public void run() {
Networking net = new Networking();
net.Login(((EditText) findViewById(R.id.textNickname)).getText().toString(), ((EditText) findViewById(R.id.textPassword)).getText().toString(), callbackInterface);
}
}).start();
}
}
And this is the function that gets called by the networking stuff, after its done:
#Override
public void LoginCallback(final boolean loginSuccess, final boolean isActivated) {
loggingIn.hide();
loggingIn = null;
final Context context = this;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (loginSuccess && isActivated) {
loggingIn.hide();
loggingIn = null;
finish();
} else if (loginSuccess == false) {
Toast.makeText(context, "Login failed! User/Password wrong", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Login failed! Account not activated", Toast.LENGTH_SHORT).show();
}
}
});
}
And this is the stack trace:
04-17 17:06:11.313 13018-13121/com.assigame.nidhoegger.assigame E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1422
Process: com.assigame.nidhoegger.assigame, PID: 13018
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6122)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:850)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.setFlags(View.java:8908)
at android.view.View.setVisibility(View.java:6036)
at android.app.Dialog.hide(Dialog.java:299)
at com.assigame.nidhoegger.assigame.Login.LoginCallback(Login.java:72)
at com.assigame.nidhoegger.assigame.Networking.Login(Networking.java:134)
at com.assigame.nidhoegger.assigame.Login$1.run(Login.java:64)
at java.lang.Thread.run(Thread.java:841)
04-17 17:06:11.719 13018-13018/com.assigame.nidhoegger.assigame E/WindowManager﹕ android.view.WindowLeaked: Activity com.assigame.nidhoegger.assigame.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42284588 G.E..... R......D 0,0-684,324} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:366)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.assigame.nidhoegger.assigame.Login.onClickLoginButton(Login.java:56)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
You are calling the loggingIn.hide() method twice, and the first time not on the UI thread. Change your code to this:
#Override
public void LoginCallback(final boolean loginSuccess, final boolean isActivated) {
final Context context = this;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (loginSuccess && isActivated) {
loggingIn.hide();
loggingIn = null;
finish();
} else if (loginSuccess == false) {
Toast.makeText(context, "Login failed! User/Password wrong", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Login failed! Account not activated", Toast.LENGTH_SHORT).show();
}
}
});
}
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy#406a6678 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:528)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.Activity.showDialog(Activity.java:2569)
at android.app.Activity.showDialog(Activity.java:2527)
at MyCode$8$4.run(MyCode.java:557)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
I am getting above exception when following code is executed. This file dialog will shown once the processing is done and progressbar reaches 100%. FileSaveDialog extends Dialog and implements OnCompletionListener
runOnUiThread(new Runnable() {
#Override
public void run() {
showDialog(error.Code());//Line 557
}
});
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
final ScrollView scrollView = new ScrollView(this);
final TextView textView = new TextView(this);
switch (id) {
// Other cases are here
case 4:
File playFile = new File(mediaPath, TEMP_WAV_FILE_NAME);
dialog = new FileSaveDialog(this, getResources(),
playFile.getAbsolutePath(), saveDiscardHandler);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// do whatever you want the back key to do
cleanUp();
}
});
break;
// Other cases are here
default:
dialog = null;
}
return dialog;
}
You must check activity isFinishing() If the activity is finishing, returns true; else returns false.
I have this code for show the MediaController, but it give me a fatal error when I'm calling to Show() method.
The MediaPlayer itself working on Service and get Intent from the MediaPlayerControl interface.
My code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaController = new MediaController(this, false);
mediaController.setMediaPlayer(mediaPlayerControl);
mediaController.setAnchorView(findViewById(R.id.mediaController));
mediaController.setEnabled(true);
mediaController.show(0);
}
//implements MediaPlayerControl interface
private MediaPlayerControl mediaPlayerControl = new MediaPlayerControl()
{
//Override the methods to send Intent to the MediaPlayer Service
....
....
};
my logcat:
07-27 11:03:07.365: E/AndroidRuntime(328): FATAL EXCEPTION: main
07-27 11:03:07.365: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radius100fm/com.example.radius100fm.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.os.Looper.loop(Looper.java:123)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 11:03:07.365: E/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:03:07.365: E/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 11:03:07.365: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 11:03:07.365: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 11:03:07.365: E/AndroidRuntime(328): at dalvik.system.NativeStart.main(Native Method)
07-27 11:03:07.365: E/AndroidRuntime(328): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
07-27 11:03:07.365: E/AndroidRuntime(328): at android.view.ViewRoot.setView(ViewRoot.java:527)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.widget.MediaController.show(MediaController.java:304)
07-27 11:03:07.365: E/AndroidRuntime(328): at com.example.radius100fm.MainActivity.onCreate(MainActivity.java:100)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 11:03:07.365: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 11:03:07.365: E/AndroidRuntime(328): ... 11 more
What is the problem with my code?
Use this method.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mediaController != null)
mediaController.show(0);
}
I got the same problem and after several hours got the solution. I did the following:
Summary:
The Activity class implements the interfaces: MediaPlayer.OnPreparedListener and MediaController.MediaPlayerControl
OnCreate. setContentView.
onStart. Create MediaPlayer and MediaController, start the listener with setOnPreparedListener and call prepare() method of MediaPlayer.
Implement method onPrepared. Link the mediaController with the mediaPlayer, start mediaPlayer and here is where the method show() is called using a handler, only when we know that mediaPlayer is ready.
My code:
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaController.MediaPlayerControl {
private static final String TAG = "AudioPlayer";
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
Log.d(TAG, "Play - onStart");
super.onStart();
mediaPlayer = new MediaPlayer();
mediaController = new MediaController(this);
mediaPlayer.setOnPreparedListener(this);
try {
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(R.raw.audio_example);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mediaPlayer.prepare();
afd.close();
} catch (IOException e) {
Log.e(TAG, "Error opening audio: " + e.getCause());
}
}
// override this method because of the OnPreparedListener interface
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "Play - onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.mediaController1));
mediaPlayer.start();
handler.post(new Runnable() {
#Override
public void run() {
mediaController.setEnabled(true);
mediaController.show(0);
}
});
}
// override these methods because of the MediaController.MediaPlayerControl interface
#Override
public boolean canPause() {
return true;
}
#Override
public boolean canSeekBackward() {
return true;
}
#Override
public boolean canSeekForward() {
return true;
}
#Override
public int getAudioSessionId() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getBufferPercentage() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
#Override
public int getDuration() {
return mediaPlayer.getDuration();
}
#Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
#Override
public void pause() {
mediaPlayer.pause();
}
#Override
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
#Override
public void start() {
mediaPlayer.start();
}
// release resources before kill the Activity
#Override
protected void onStop() {
Log.d(TAG, "Play - onStop");
super.onStop();
if (mediaPlayer != null) {
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
} }
Ok, so finely I found the answer.
The problem is in the line:
mediaController.show(0);
Because it called in onCreate() and the app still not activated.
Simple tried, add bottom that callingmediaController.show(0); when it clicked, and the app work perfect.
So now I have to call this line after the app get activate.
I'm tried onStart() and onResume() and it not working. The same error logCat.
How can I fix that??
I needed to show the MediaController to a already running MediaPlayer, so I couldn't set the OnPreparedListener like e_v_e said.
With the answer to this question: Can't fix MediaController.show() exception I discovered that the show method is been called before all the activity lifecycle methods were called. The solution proposed there(setting a delay to show) works, but to avoid the delay, you can place the show inside the onAttachedToWindow method, that is called after all the activity lifecycle methods.
Just put the mediaPlayer.prepare() and mediaPlayer.start() in a thread like handler or AsyncTask and done.
If you are using kotlin and anko lib, you can put it in doAsync {}.
When i start my app and press stop or pause the android app will crash. It works fine if you press play first and then stop or pause. I searched on google and stackoverflow but i couldn't find much about it. I think the problem is because of a NullPointerException but since i'm new too java it doesn't tell me much about the problem
The code:
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class myMain extends Activity implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, OnClickListener {
private String TAG = getClass().getSimpleName();
private MediaPlayer mp= null;
private Button play;
private Button pause;
private Button stop;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
stop();
}
});
}
private void play() {
Uri myUri = Uri.parse("url");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
private void stop() {
mp.stop();
}
#Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
public boolean onError(MediaPlayer mp, int what, int extra) {
StringBuilder sb = new StringBuilder();
sb.append("Media Player Error: ");
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
sb.append("Not Valid for Progressive Playback");
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
sb.append("Server Died");
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
sb.append("Unknown");
break;
default:
sb.append(" Non standard (");
sb.append(what);
sb.append(")");
}
sb.append(" (" + what + ") ");
sb.append(extra);
Log.e(TAG, sb.toString());
return true;
}
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The errors:
02-11 20:45:43.837: D/AndroidRuntime(338): Shutting down VM
02-11 20:45:43.837: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 20:45:43.857: E/AndroidRuntime(338): FATAL EXCEPTION: main
02-11 20:45:43.857: E/AndroidRuntime(338): java.lang.NullPointerException
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain.stop(myMain.java:95)
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain.access$2(myMain.java:94)
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain$3.onClick(myMain.java:55)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.view.View.performClick(View.java:2485)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.view.View$PerformClick.run(View.java:9080)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Handler.handleCallback(Handler.java:587)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Looper.loop(Looper.java:123)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 20:45:43.857: E/AndroidRuntime(338): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 20:45:43.857: E/AndroidRuntime(338): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 20:45:43.857: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 20:45:43.857: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 20:45:43.857: E/AndroidRuntime(338): at dalvik.system.NativeStart.main(Native Method)
In your stop() method you access the mp variable. However, the mp variable is null until you press play (and the play() method is called). So when you try to access the null variable a NullPointerException is thrown.
A very simple way to stop the NullPointerException is to do something like this:
private void pause() {
if(mp!=null) mp.pause();
}
private void stop() {
if(mp!=null) mp.stop();
}
Of course this solution doesn't account for cases where pause or stop is called twice. Take a look at the MediaPlayer documentation for more info on state management.