Reputation: 590
I have an app that plays music when opened. But when I stop the music, and use the back button, I get an Exception and the app forcecloses. This is my code:
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.media_player_layout);
final Uri uri = getIntent() != null ? getIntent().getData() : null;
final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
Button play = (Button) findViewById(R.id.play);
Button stop = (Button) findViewById(R.id.stop);
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
musicPlayer.start();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(musicPlayer.isPlaying()){
musicPlayer.pause();
}
else {musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
musicPlayer.start();
}}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (musicPlayer.isPlaying()) {
musicPlayer.stop();
musicPlayer.release();
}
}
});
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (musicPlayer != null && musicPlayer.isPlaying()) {
musicPlayer.stop();
musicPlayer.release();
musicPlayer = null;
finish();
}
}
});
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayer.this);
builder.setTitle("Error");
builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
builder.setIcon(R.drawable.holo_dark_action_info);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}}
public void onBackPressed () {
if(musicPlayer.isPlaying()) {
musicPlayer.stop();
musicPlayer.release();
}
musicPlayer = null;
finish();
}
}
and this is my logcat:
09-30 18:00:33.106 23141-23141/com.tproductions.Openit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tproductions.Openit, PID: 23141
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.tproductions.Openit.MusicPlayer.onBackPressed(MusicPlayer.java:76)
How can I make it working ? I've tried the if-else statement but it's not working either.
Upvotes: 0
Views: 469
Reputation: 23269
It is not valid to call isPlaying()
after you have called release()
on a MediaPlayer
.
You release it after hitting the stop button, and then call isPlaying()
in onBackPressed()
- this causes your IllegalStateException
. You should only call release()
when there is absolutely nothing else you want to do with your Mediaplayer
According to the docs:
It is considered good practice to call this method when you're done using the MediaPlayer.
However in this case you are not done with it yet.
To fix, remove the following call to release()
:
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (musicPlayer.isPlaying()) {
musicPlayer.stop();
// musicPlayer.release(); <- remove this
}
}
});
As a side note, it is simpler to just call release()
once in your activity lifecycle methods, like in onStop()
or onPause()
. If you pepper your code with calls to release()
you will run into problems like this.
Upvotes: 0
Reputation: 3010
Use Like below code onBackPressed()
AudioManager.isMusicActive()
does the job you want, have a closer look here for details: AudioManager
here is an example:
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or do it not
}
Upvotes: 1