2013-07-09 47 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)

檢查這對你有幫助。