2013-07-09 46 views
0

我已经创建了一个活动,其中包括在它。当媒体播放器,我开始活动,音乐开始play.But歌曲完成后,当我上单击模拟器上后退按钮,会显示一个错误(IllegellStateException) 。如何在Android的停止活动?

这是我的代码。

谢谢先进。

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); 
} 

}

+0

把日志消息 –

+0

这些答案都不够好吗? http://stackoverflow.com/questions/17522185/how-to-resume-my-audio-in-android http://stackoverflow.com/questions/17545166/how-to-resume-activity-with-audio-in -android http://stackoverflow.com/questions/17524681/resume-song-in-android –

回答

0
if(mp!= null) 
{ 

    mp.pause(); 
} 

内部if ((keyCode == KeyEvent.KEYCODE_BACK))上面代码段似乎是问题。你应该检查玩家是否在玩。如果正在播放,则只能暂停播放。

0

我认为这个问题是在这里:

@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(); 
} 

欲了解更多信息阅读here

0

这里的onStop()的实现,节省了说明草案,以永久存储的内容:

@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. 
      ); 
} 

虽然onPause()方法onStop()之前调用,你应该使用onStop()执行更大,更多的CPU密集关闭操作,例如将信息写入数据库。

从Android开发者页面:http://developer.android.com/training/basics/activity-lifecycle/stopping.html

4

使用finish();Activity类停止活动并返回

1

试试这个

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);} 
0

您在的onPause得到(IllegelStateException) ()

要解决此问题,你需要把,

mp = null; 

后,

mp.stop(); 
mp.release(); 

,因为当你检查MP!= NULL那个时候的MediaPlayer已经释放,但不。 所以,如果条件是正确的,转到mp.pause()MP已经释放,这就是为什么你会得到错误。

您还需要更改名称MediaPlayer实例onCompletion()

public void onCompletion(MediaPlayer mp) 

,因为它是为停止()释放()使用onCompletion()的熔点。 所以,改变熔点其他(如:mp1)

检查这对你有帮助。