如何关闭VideoView活动(目前有preSS回来两次) [英] How to close a VideoView Activity (currently have to press back twice)

查看:249
本文介绍了如何关闭VideoView活动(目前有preSS回来两次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的活动,以preVIEW全屏视频。活动的布局是:

I have a simple Activity to preview a video in FullScreen. The layout of the Activity is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black"
                android:fillViewport="true">

    <VideoView
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:scaleType="fitCenter"/>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_height"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_alignParentBottom="true"
        <Button android:id="@+id/buttonClose"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:background="@null"
                android:textSize="18sp"
                android:textColor="@color/white"
                android:text="@string/close"/>
    </RelativeLayout>
</RelativeLayout>

相关活动code是:

The relevant Activity code is:

public class VideoFullscreenActivity extends Activity {
    private VideoView video;
    private Button closeButton;
    private MediaController mediaController;
    private boolean toClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_fullscreen);

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        video = (VideoView) findViewById(R.id.video);
        closeButton = (Button) findViewById(R.id.buttonClose);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.d("VideoPreview", "onClick Close Button");
                VideoFullscreenActivity.super.onBackPressed();
            }
        });

        Bundle bundle = getIntent().getParcelableExtra("bundle");

        if (bundle != null) {
            Uri videoUri = bundle.getParcelable("data");
            mediaController = new MediaController(this);
            mediaController.setAnchorView(video);
            video.setMediaController(mediaController);
            video.setVideoURI(videoUri);
            video.requestFocus();
            video.start();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                finish();
        }
        return (super.onOptionsItemSelected(menuItem));
    }

    @Override
    protected void onResume() {
        Log.i("VideoPreview", "Resume");
        video.resume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.i("VideoPreview", "Pause");
        video.suspend();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.i("VideoPreview", "Destroy");
        video.stopPlayback();
        super.onDestroy();
    }

}

当我preSS的关闭按钮,在第一时间,顺序为:

When I press the "Close" button the first time, the sequence is:

D/VideoPreview﹕ onClick Close Button
I/VideoPreview﹕ Pause
I/VideoPreview﹕ Resume
I/VideoPreview﹕ Destroy

在这个阶段,活动不接近,重新启动并在视频开始重新播放。要关闭活动,我不得不preSS的关闭按钮一次。

At this stage, the Activity doesn't close, it restarts and the video starts playing again. To close the Activity, I have to press the "Close" button again.

当我preSS的关闭按钮,第二次,顺序是:

When I press the "Close" button the second time, the sequence is:

D/VideoPreview﹕ onClick Close Button
I/VideoPreview﹕ Pause
I/VideoPreview﹕ Destroy

现在,活动将关闭。

这是我在计算器上发现的一些相关的问题:

Some relevant questions that I found on StackOverflow:

  • Android - VideoView requires to press BACK twice, in order to exit (adding the onKeyDown function didn't help)
  • How to close Landscape VideoView Activity properly? (I have overridden the onDestroy, onResume and onPause methods but that hasn't solved the issue)

可能有人请给我解释一下它是什么,我做不正确(或不理解)。

Could someone please explain to me what is it that I am doing incorrectly (or failing to understand).

感谢

推荐答案

在code关闭活动是正确的。问题是在推出VideoFullscreenActivity活动。违规件code的是:

The code to close the activity was correct. The problem was in the Activity which launched the VideoFullscreenActivity. The offending piece of code was:

    <VideoView
            android:id="@+id/tag_attach_video"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"/>

VideoFullscreenActivity做了呼吁onBack pressed()结束,但在早期活动的VideoView仍然活跃,回来必要pressing。

VideoFullscreenActivity did finish on calling onBackPressed(), but the VideoView in the earlier Activity was still active which necessitated pressing back again.

我改变了VideoView到ImageView的和所使用的占位符图像,以指示所连接的数据是一个视频的事实。这解决了这个问题。

I changed the VideoView to an ImageView and used a placeholder image to indicate the fact that the attached data is a video. That solved the problem.

这篇关于如何关闭VideoView活动(目前有preSS回来两次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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