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); 
    } 

그리고 :

은 그 때 나는이 시도. 다시 버튼을 두 번 눌러야합니다.

편집 : 이것은 가장 관련있는 소스 코드입니다. 그러나 비디오는 인터넷에서 재생되며, Mediacontroller를 사용하지 않고 대신 자신의 레이아웃을 정의하고 videoview 콘트롤에 연결합니다.

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

당신은 u는이 비디오를 재생하는 r에 어떻게 보일 수 있는가? –

+0

코드가 추가되었습니다. –

답변

0

제거이,

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); 
} 

그리고 추가는, 저를 도우려고 주셔서 감사합니다.
startActivity(intent);을 두 번 호출했는데, 그 이유는 뒤로 버튼을 두 번 눌러야하는 이유입니다. 나를 위해 잘 작동 아래

0

당신은 onBackPressed 대신 KeyEvent의를 무시 시도 할 수 있습니다. 귀하의 코드가 올바르게 보이기 때문입니다.

@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 commenters가 이미 답변으로 표시된 질문에 응답하지 않은 경우 오늘날보다 훨씬 유익하지 않습니다. 사람들은 문제를 해결하는 첫 번째 답변을 정확하다고 표시하는 경향이 있지만 실제/최상의 해결책은 아닙니다. –

관련 문제