2016-11-02 58 views
0

當我啓動應用程序並單擊按鈕時,Media Player可以正常工作。但是,當我點擊主頁按鈕或當我轉到另一個活動時,當我回到MainActivity時,我無法再播放媒體文件。這裏是我的代碼:如何在其他活動支持後播放媒體播放器?

public void playMedia(View view){ 

    if(mp.isPlaying()){ 
     mp.pause(); 
    } 
    else 
     mp.start(); 
} 

@Override 
protected void onPause(){ 
    super.onPause(); 
    SOSPlayer.pause(); 
} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    SOSPlayer.seekTo(0); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    SOSPlayer.stop(); 
    SOSPlayer.release(); 
} 
@Override 
protected void onStop() { 
    super.onStop(); 
    SOSPlayer.stop(); 
} 
+0

在onResume()中啓動播放器。 – Archana

回答

0

我的事情你打電話playMedia方法上的佈局爲:

<Button 
... 
android:onClick="playMedia" 
.. 
/> 

你需要的是獲得距上一次活動後會再播放媒體..你需要開始該活動的結果與startActivityForResult並聽取反饋onActivityResult再次播放媒體。這是一個簡單的實現:

private static final int REQUEST_ID = 0x0001; 
    private void startActivity() { 
     Intent intent = new Intent(getApplicationContext(), DifferentActivity.class); 
     startActivityForResult(intent, REQUEST_ID); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
      case REQUEST_ID: 
       playMedia(null); 
       break; 
      default: 
       super.onActivityResult(requestCode, resultCode, data); 
     } 

    }