Android的 - VideoView要求preSS返回两次,以退出 [英] Android - VideoView requires to press BACK twice, in order to exit

查看:646
本文介绍了Android的 - VideoView要求preSS返回两次,以退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题是,当我想退出这个活动,并返回到previous,我两次单击后退按钮,以此来回报回来。如果我点击一次,在视频开始再次播放,并仅在第二次尝试,我允许退出屏幕。

然后我尝试这样的:

  @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果(键code == KeyEvent.KEY code_BACK){
            Log.d(Constants.LOG_TAG,回$ P $在录像机pssed);
            完();
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }

和,虽然我在logcat中看到回pressed的视频播放器,该活动并不退出。我还是应该preSS两次后退按钮。

编辑:这是最相关的(我相信)来源$ C ​​$ C。但是请注意,视频是从互联网上播放,我没有使用的MediaController,而不是我定义我自己的布局,并链接到videoview conrols。

 公共类的VideoPlayer扩展活动实现OnClickListener,OnCompletionListener,
        OnSeekBarChangeListener,在preparedListener,OnTouchListener {
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.video_view);        //获取点击视频的位置,从视频的URL的ArrayList。
        selectedVideo =为getPosition();        //播放按钮
        玩=(的ImageButton)findViewById(R.id.play);
        play.setOnClickListener(本);        videoView =(VideoView)findViewById(R.id.videoView);
        videoView.setOnCompletionListener(本);
        videoView.setOn preparedListener(本);
        videoView.setOnTouchListener(本);        //链接直接打
        字符串路径= videoUris.get(selectedVideo);
        videoView.setVideoPath(的getPath(路径));
    }
    / **
     *播放或暂停当前视频文件。
     *
     *如果视频已暂停,则调用此方法应该启动它。如果视频已在播放,那么
     *视频应该暂停。
     * /
    私人无效播放(){
        如果(!isVideoStarted){
            isVideoStarted = TRUE;
            videoView.start();
            play.setImageResource(R.drawable.video_pause);
            videoSeekBar.post(updateSeekBarRunnable);
        }否则如果(isVideoStarted){
            isVideoStarted = FALSE;
            暂停();
        }
    }    / **
     *开始播放具有指定URI的视频文件。
     * /
    私人无效startPlayback(){
        字符串路径= videoUris.get(selectedVideo);
        videoView.setVideoPath(的getPath(路径));
        videoView.start();
    }    / **
     *停止当前播放的视频。 (搜索栏位置重置为开头,0)
     * /
    私人无效stopPlayback(){
        videoView.stopPlayback();
    }    / **
     *暂停当前播放的视频。 (搜索栏仍然在其位置。)
     * /
    私人无效暂停(){
        videoView.pause();
    @覆盖
    在prepared(MediaPlayer的MP)公共无效{
        玩();
    }
}


解决方案

感谢您想帮助我。结果
原来,我打电话 startActivity(意向); 两次,这就是我为什么要两次$ $ p PSS后退按钮

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

解决方案

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.

这篇关于Android的 - VideoView要求preSS返回两次,以退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆