2013-08-23 96 views
0

大家好我有一個視頻集在我的應用程序中作爲背景。當應用程序啓動主菜單中的視頻播放時,一切正常。現在,當我選擇進入下一個活動時,視頻停止和下一個活動開始,當用戶完成此活動並按下後退按鈕以轉到主菜單時,視頻應該再次播放,但它不會「噸。希望有人能幫助我。這裏是我的代碼:當按下後退按鈕時播放視頻

public class MainActivity extends Activity { 

     VideoView animation; 
private MediaController mc; 
MediaPlayer mp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mp = MediaPlayer.create(this, R.raw.leftbanktwo); 
    mp.setLooping(true); 
    VideoView animation = (VideoView) findViewById(R.id.imageAnimation); 
    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.cartoon); 
    mc = new MediaController(this); 
    animation.setMediaController(mc); 
    animation.requestFocus(); 
    animation.setVideoURI(uri); 
    animation.start(); 
} 

回答

1

試試這個

@Override 
    protected void onResume() { 
     super.onResume(); 
     if(mp!=null){ 
      mp.reset(); 
      mp.start(); 
     } 

    } 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     if(mp!=null && mp.isPlaying()){ 
      mp.pause(); 
     } 
    } 
+0

感謝,似乎已經解決了我的問題! – Allrounder

2

這是因爲你的onCreate()方法不再次調用時用戶返回到第一Activity。如果你想讓它像你所描述的那樣工作,那麼在onResume()方法中放入啓動視頻的代碼。

此外,我會建議檢查出Activity Lifecycle

相關問題