【问题标题】:Android - VideoView requires to press BACK twice, in order to exitAndroid - VideoView 需要按两次 BACK 才能退出
【发布时间】:2012-06-11 08:03:18
【问题描述】:

我有一个显示不同视频文件的活动。当我单击一个视频文件时,我被带到另一个活动,其中一个 VideoView 播放视频。

我的问题是,当我想退出此活动并返回上一个活动时,我应该单击两次后退按钮才能返回。如果我只单击一次,视频就会再次开始播放,并且只有在第二次尝试时我才被允许退出屏幕。

然后我尝试了这个:

@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 控件。

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

【问题讨论】:

  • 你能展示一下你是如何播放这个视频的吗?

标签: android android-layout android-emulator android-widget android-ndk


【解决方案1】:

下面的代码对我来说很好用:

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

【讨论】:

  • @AndyRes 如果 stackoverflow 评论者没有回答已标记为已回答的问题,那么它的信息量将比现在少得多。人们倾向于将解决问题的第一个答案标记为正确,但这可能不是真正/最佳的解决方案。
【解决方案2】:

感谢您尝试帮助我。
原来我打了两次startActivity(intent);,所以我应该按两次返回键。

【讨论】:

    【解决方案3】:

    您能否尝试覆盖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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 2013-05-10
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2020-09-24
      相关资源
      最近更新 更多