Reputation: 145
I have created an activity which includes media player in it.When I start activity, the music begins to play.But when the song is complete and when i click on back button on the emulator, it displays an error of (IllegellStateException
).
Here is my code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
init();
imgVw.setImageResource(R.raw.teddy_two);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
Log.e("Song is playing","in Mediya Player ");
Log.e("Current ","Position -> " + length);
mp.setLooping(false);
mp.start();
btnChapter.setEnabled(false);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
// TODO Auto-generated method stub
mp.stop();
mp.release();
btnChapter.setEnabled(true);
System.out.println("Music is over and Button is enable !!!!!!");
}
});
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPause()
{
super.onStop();
SharedPreferences. Editor prefsEdit = prefs.edit();
int position = mp.getCurrentPosition();
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("Activity is Resume !!!");
int position = prefs.getInt("mediaPosition", 0);
mp.seekTo(position);
mp.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
if(mp!= null)
{
mp.pause();
}
//finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Upvotes: 0
Views: 10351
Reputation: 476
Use finish();
in Activity class to stop Activity and return.
Upvotes: 4
Reputation: 4349
You get (IllegelStateException) on the onPause().
To resolve this problem you need to put,
mp = null;
after,
mp.stop();
mp.release();
because when you check mp!=null that time MediaPlayer already release but not null. so, if condition is correct and goto mp.pause() but mp already release that's why you get error.
Also you need to change name of MediaPlayer instance in onCompletion().
public void onCompletion(MediaPlayer mp)
because it is used mp of onCompletion() for stop() and release(). so, change mp to other(like: mp1).
check this is helpfull for you.
Upvotes: 0
Reputation: 4108
Try this
MediaPlayer player;
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "service started", 100).show();
if(player.isPlaying())
player.stop();
else
player.start();
return super.onStartCommand(intent, flags, startId);}
Upvotes: 1
Reputation: 1404
if(mp!= null)
{
mp.pause();
}
The above code snippet inside if ((keyCode == KeyEvent.KEYCODE_BACK))
seems to be the problem. You should check whether the player is playing or not. If it is playing then only pause should be called.
Upvotes: 0
Reputation: 1554
Here's an implementation of onStop() that saves the contents of a draft note to persistent storage:
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
// Save the note's current draft, because the activity is stopping
// and we want to be sure the current note progress isn't lost.
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update(
mUri, // The URI for the note to update.
values, // The map of column names and new values to apply to them.
null, // No SELECT criteria are used.
null // No WHERE columns are used.
);
}
Although the onPause()
method is called before onStop()
, you should use onStop()
to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
from android developer page: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
Upvotes: 0
Reputation: 271
I think the problem is here:
@Override
public void onPause()
{
super.onPause();//should be onPause and not onStop
SharedPreferences. Editor prefsEdit = prefs.edit();
int position = mp.getCurrentPosition();
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
For more info read here.
Upvotes: 0