2

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();
    }
}
Andy Res
  • 15,963
  • 5
  • 60
  • 96

3 Answers3

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);
}
Tungken
  • 1,917
  • 1
  • 15
  • 18
  • 2
    @AndyRes If stackoverflow commenters didn't answer questions already marked as answered, it would be a lot less informative than it is today. People tend to mark the first answer that solves their problem as correct, but it might not be the real/best solution. – Zac Sweers Aug 09 '14 at 00:30
0

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();
}
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

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.

Andy Res
  • 15,963
  • 5
  • 60
  • 96