2016-09-30 62 views
0

我有一個作爲背景播放的視頻視圖。當應用程序失去焦點並在應用程序重新聚焦時恢復視頻時,如何暫停它?這是我的代碼。基於焦點的暫停/恢復視頻

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

    VideoView videoView = (VideoView) findViewById(R.id.launcherVideo); 
    Uri src = Uri.parse("android.resource://com.package/raw/video"); 
    videoView.setVideoURI(src); 

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 

     @Override 
     public void onPrepared(MediaPlayer mp) { 

      mp.setVolume(0, 0); 
      mp.setLooping(true); 
     } 
    }); 

    //videoView.setMediaController(new MediaController(this)); 

    videoView.start(); 
} 

回答

1
override onPause() method and write code for pause on that method and override onResume() method and write code for resume on that method 
+0

謝謝,我是初學者,雖然。我不知道如何編寫暫停和恢復代碼。 – Skankhunt42

+0

mediaplayer.pause();和mediaplayer.start(); –

+0

我不確定這是否正確,但這是我想出的。 '@Override protected void onPause(){ super.onPause(); mediaplayer.pause(); } @Override protected void onResume(){ super.onResume(); mediaplayer.start(); }' – Skankhunt42