使用Android Presentation Class在辅助屏幕上显示视频

时间:2019-03-01 23:40:33

标签: java android video dialog android-videoview

我正在开发一个应用,其中的功能之一是具有两个按钮,当这些按钮被触摸时,它们将在第二台显示器上全屏显示两个本地存储的视频之一。由于我使用的平板电脑具有HDMI输出端口,因此显示器直接通过HDMI电缆连接。无需通过搜索或停止来控制视频播放。再次按下该按钮,视频可以简单地重新开始(搜索为0)。如果用户正在播放video1并按下video2按钮,则假定video1将立即停止并且video2将开始。

当前的实现(下面包含一个存根)基本上可以按预期工作。当我第一次连接辅助显示器时,启动应用程序并按任一按钮,关联的视频开始在VideoView中播放。如果我再次单击该按钮或单击另一个按钮,则关联的视频将从头开始播放。

我遇到的错误发生在以下时间:我使用“后退”按钮退出应用程序,或者使用“概述”按钮使应用程序后台运行,或者通过向内滑动应用程序退出应用程序应用概述。下次我打开该应用程序并单击一个视频按钮时,辅助显示器上什么也没有发生。它只是作为平板电脑主显示屏的一面镜子。如果我拆下并重新连接辅助显示器,即使在应用程序打开的情况下,按钮也会按预期开始工作。这使我相信,在应用程序暂停或停止时,我没有正确清理Presentation,MediaRouter或VideoView。任何帮助将不胜感激。如果添加主要活动代码有帮助,请告诉我。

import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.MediaRouter;
import android.content.Context;
import android.app.Presentation;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.util.Log;

import java.util.ArrayList;
import java.util.Map;


public class GuiFragment extends Fragment{
    private MediaRouter mMediaRouter;
    private VideoPresentation mPresentation;

    public GuiFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("GuiFragment", "onCreate");
        super.onCreate(savedInstanceState);

        // Get the media router service.
        mMediaRouter = (MediaRouter) getActivity().getSystemService(Context.MEDIA_ROUTER_SERVICE);
    }

    @Override
    public void onPause() {
        Log.e("GuiFragment", "onPause");
        // Be sure to call the super class.
        super.onPause();

        // Dismiss the presentation when the activity is not visible.
        stopVideo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e("GuiFragment", "onCreateView");
        View rootView = inflater.inflate(R.layout.fragment_connection_gui, container, false);

        // Initialize Button objects
        Button video1Button = (Button) rootView.findViewById(R.id.vid1_button);
        Button video2Button = (Button) rootView.findViewById(R.id.vid2_button);

        video1Button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        Log.e("GuiFragment", "video1 button event");
                        int duration = Toast.LENGTH_SHORT;
                        MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
                        if (route != null) {
                            Log.e("GuiFragment", "Obtained Video Media Route");
                            Display presentationDisplay = route.getPresentationDisplay();
                            if (presentationDisplay != null) {
                                Log.e("GuiFragment", "Obtained Presentation Display, Presenting Video");
                                //if (mPresentation == null)
                                //{
                                    Log.e("GuiFragment", "Video1: No presentation, create a new one");
                                    mPresentation = new VideoPresentation(getActivity(), presentationDisplay,
                                            Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.bigbuckbunny));
                                    mPresentation.show();
                                //}
                                //else
                                //{
                                //    Log.e("GuiFragment", "Video1: Presentation exists, update URI");
                                //    mPresentation.stopVideoView();
                                //    mPresentation.updateVideo(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.bigbuckbunny));
                                //}
                            }
                            else {
                                Toast toast = Toast.makeText(getActivity(), "No Display", duration);
                                toast.show();
                            }
                        }
                        else {
                            Toast toast = Toast.makeText(getActivity(), "No MediaRouter", duration);
                            toast.show();
                        }
                        break;
                    default:
                        break;
                }
                /* This causes onClick not to be called. You would need it for accessibility options
                   but we don't really care about that in this app */
                return true;
            }
        });

        video2Button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        Log.e("GuiFragment", "video2 button event");
                        int duration = Toast.LENGTH_SHORT;
                        MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
                        if (route != null) {
                            Log.e("GuiFragment", "Obtained Video Media Route");
                            Display presentationDisplay = route.getPresentationDisplay();
                            if (presentationDisplay != null) {
                                Log.e("GuiFragment", "Obtained Presentation Display, Presenting Video");
                                //if (mPresentation == null)
                                //{
                                    Log.e("GuiFragment", "Video2: No presentation, create a new one");
                                    mPresentation = new VideoPresentation(getActivity(), presentationDisplay,
                                            Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.bigbuckbunny));
                                    mPresentation.show();
                                //}
                                //else
                                //{
                                //    Log.e("GuiFragment", "Video2: Presentation exists, update URI");
                                //    mPresentation.stopVideoView();
                                //    mPresentation.updateVideo(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.bigbuckbunny));
                                //}
                            }
                            else {
                                Toast toast = Toast.makeText(getActivity(), "No Display", duration);
                                toast.show();
                            }
                        }
                        else {
                            Toast toast = Toast.makeText(getActivity(), "No MediaRouter", duration);
                            toast.show();
                        }
                        break;
                    default:
                        break;
                }
                /* This causes onClick not to be called. You would need it for accessibility options
                   but we don't really care about that in this app */
                return true;
            }
        });

        // Inflate the layout for this fragment
        return rootView;
    }

    private void stopVideo() {
        Log.e("GuiFragment", "stopVideo()");
        if (mPresentation != null) {
            Log.e("GuiFragment", "Stopping video1 playback");
            mPresentation.stopVideoView();
            mPresentation.dismiss();
            mPresentation = null;
        }
    }



    private final static class VideoPresentation extends Presentation {

        VideoView mVideoView;
        Context mContext;
        Uri mVideo;

        public VideoPresentation(Context context, Display display, Uri video) {
            super(context, display);
            mContext = context;
            mVideo = video;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.e("VideoPresentation", "onCreate");
            // Be sure to call the super class.
            super.onCreate(savedInstanceState);

            // Get the resources for the context of the presentation.
            // Notice that we are getting the resources from the context of the presentation.
            Resources r = getContext().getResources();

            // Inflate the layout.
            setContentView(R.layout.presentation_with_media_router_activity);

            mVideoView = (VideoView) findViewById(R.id.video_view);
            mVideoView.requestFocus();
            mVideoView.setVideoURI(mVideo);

            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                public void onPrepared(MediaPlayer mediaPlayer) {
                    mVideoView.start();
                }
            });
        }

        @Override
        protected void onStart() {
            Log.e("VideoPresentation", "onStart");
            // Be sure to call the super class.
            super.onStart();
        }

        @Override
        protected void onStop() {
            Log.e("VideoPresentation", "onStop");
            // Be sure to call the super class.
            super.onStop();
        }

        @Override
        public void onDetachedFromWindow() {
            Log.e("VideoPresentation", "onDetachedFromWindow");
            // Be sure to call the super class.
            super.onDetachedFromWindow();
        }

        public void stopVideoView() {
            mVideoView.stopPlayback();
        }

        public void updateVideo(Uri video) {
            mVideo = video;
            mVideoView.requestFocus();
            mVideoView.setVideoURI(mVideo);
        }

        public VideoView getVideoView() {
            return mVideoView;
        }
    }
}

0 个答案:

没有答案