1

我有一個顯示不同視頻文件的活動。當我點擊一個視頻文件時,我被帶到另一個活動,在那裏一個VideoView播放視頻。Android - VideoView需要按兩次BACK才能退出

我的問題是,當我想退出此活動並返回到上一個時,我應該單擊兩次後退按鈕,以便返回。如果我只點擊一次,視頻就會再次開始播放,只有在第二次嘗試我才能退出屏幕時。

然後我嘗試這樣的:

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      Log.d(Constants.LOG_TAG, "back pressed in videoplayer"); 
      finish(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

而且,雖然我在logcat的「視頻播放器回壓」,該活動不會退出見。我仍然應該按兩次後退按鈕。

編輯:這是最相關的(我相信)的源代碼。但請注意,視頻是從互聯網播放的,我沒有使用Mediacontroller,而是定義自己的佈局並鏈接到videoview conrols。

public class VideoPlayer extends Activity implements OnClickListener, OnCompletionListener, 
     OnSeekBarChangeListener, OnPreparedListener, OnTouchListener { 


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

     // Gets the position of clicked video, from an ArrayList of video urls. 
     selectedVideo = getPosition(); 

     // the play button 
     play = (ImageButton) findViewById(R.id.play); 
     play.setOnClickListener(this); 

     videoView = (VideoView) findViewById(R.id.videoView); 
     videoView.setOnCompletionListener(this); 
     videoView.setOnPreparedListener(this); 
     videoView.setOnTouchListener(this); 

     // the url to play 
     String path = videoUris.get(selectedVideo); 
     videoView.setVideoPath(getPath(path)); 
    } 


    /** 
    * Play or Pause the current video file. 
    * 
    * If the video is paused, then invoking this method should start it. If the video is already playing, then the 
    * video should pause. 
    */ 
    private void play() { 
     if (!isVideoStarted) { 
      isVideoStarted = true; 
      videoView.start(); 
      play.setImageResource(R.drawable.video_pause); 
      videoSeekBar.post(updateSeekBarRunnable); 
     } else if (isVideoStarted) { 
      isVideoStarted = false; 
      pause(); 
     } 
    } 

    /** 
    * Start playing back a video file with the specified Uri. 
    */ 
    private void startPlayback() { 
     String path = videoUris.get(selectedVideo); 
     videoView.setVideoPath(getPath(path)); 
     videoView.start(); 
    } 

    /** 
    * Stops the currently playing video. (The SeekBar position is reset to beginning, 0.) 
    */ 
    private void stopPlayback() { 
     videoView.stopPlayback(); 
    } 

    /** 
    * Pause the currently playing video. (The SeekBar remains in its position.) 
    */ 
    private void pause() { 
     videoView.pause(); 


    @Override 
    public void onPrepared(MediaPlayer mp) {   
     play(); 
    } 
} 
+0

可以顯示ü[R怎麼玩這個視頻? –

+0

添加了代碼。 –

回答

0

感謝您嘗試幫助我。
事實證明,我打電話startActivity(intent);兩次,這就是爲什麼我應該按兩次後退按鈕。

0

您可以嘗試覆蓋onBackPressed而不是KeyEvent。因爲你的代碼似乎看起來是正確的。

刪除此,

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     Log.d(Constants.LOG_TAG, "back pressed in videoplayer"); 
     finish(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

並添加,

@Override 
public void onBackPressed() { 
    finish(); 
} 
0

低於做工精細代碼對我來說:

  try 
      { 
      MediaController mc = new MediaController(YourActivity.this); 
      mc.setAnchorView(vd); 
      Uri uri = Uri.parse(YourURL); 
      vd.setMediaController(mc); 
      vd.setVideoURI(uri); 
      //vd.start(); 
      }catch(Exception e) 
      { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      vd.requestFocus(); 
      vd.start(); 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    // TODO Auto-generated method stub 
    if(keyCode == KeyEvent.KEYCODE_BACK) 
    { 
     finish(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
+0

感謝您的努力,但問題已答覆已標記爲已接受。 –

+1

@AndyRes如果stackoverflow評論者沒有回答已經被標記爲已回答的問題,那麼它的信息量就會比現在少很多。人們傾向於將解決問題的第一個答案標記爲正確,但這可能不是真正的/最佳的解決方案。 –

相關問題