Android - VideoView requires to press BACK twice, in order to exit

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:38:53

问题


I have an activity with different video files displayed. When I click a video file, I'm taken to another activity, where a VideoView plays the video.

My issue is that when I want to exit this activity and return back to previous, I should click twice the back button, in order to return back. If I click only once, the video starts playing once again, and only at the second attempt I'm allowed to exit the screen.

Then I tried this:

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

And, although I see in the logcat "back pressed in video player", the activity does not exit. I still should press twice the back button.

EDIT: This is the most relevant (I believe) source code. Note however, that the video is played from the internet, and I'm not using the Mediacontroller, instead I'm defining my own layout and link to 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();
    }
}

回答1:


Code below work fine for me:

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



回答2:


Thank you for trying to help me.
It turned out that I was calling startActivity(intent); twice, and that is why I should press the back button twice.




回答3:


Can you try overriding the onBackPressed instead of KeyEvent. Because your code seems to look correct.

Remove this,

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

And add,

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


来源:https://stackoverflow.com/questions/10928900/android-videoview-requires-to-press-back-twice-in-order-to-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!