android - 如何关闭 VideoView Activity(目前必须按两次返回)

标签 android android-activity android-mediaplayer

我有一个简单的 Activity 可以全屏预览视频。 Activity 的布局是:

<?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>

相关的 Activity 代码是:

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

}

当我第一次按下“关闭”按钮时,顺序是:

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

在这个阶段,Activity 不会关闭,它会重新启动并且视频会再次开始播放。要关闭 Activity ,我必须再次按下“关闭”按钮。

当我第二次按下“关闭”按钮时,顺序是:

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

现在, Activity 关闭。

我在 StackOverflow 上发现的一些相关问题:

有人可以向我解释我做错了什么(或未能理解)吗?

谢谢

最佳答案

你可以调用activity finish()强制关闭activity

关于android - 如何关闭 VideoView Activity(目前必须按两次返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679583/

相关文章:

android - android web 浏览器使用什么来播放音频流?媒体播放器还是别的?

Android RTSP 流式传输失败

android - Contentresolver.delete() 不工作 android

java - 如何在android中使用volley获取Json对象中的Json数组并在listview代码和Xml Avilavble中设置此json数据

android - 从自定义适配器更新 menuItem 图标

java - 选中一定数量的复选框后禁用复选框

android - Activity 上的左/右滑动效果

android - 蓝牙 Bluez : Unable to create crypto contex

android - 单播发送 UDP 不起作用,但广播发送 UDP 可以

android - 为什么 Android Studio 告诉我使用 getSupportActionBar() 而不是 getActionBar()?