java – VideoView onTouch事件:暂停/恢复视频,并显示/隐藏MediaController和ActionBar

前端之家收集整理的这篇文章主要介绍了java – VideoView onTouch事件:暂停/恢复视频,并显示/隐藏MediaController和ActionBar前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题总结:

1)如何首先将视频暂停显示,而不是立即播放?

2)如何暂停/取消暂停视频触摸,还可以隐藏/显示ActionBar和MediaController.

我会感谢任何建议.谢谢! (附有相关规范)

更新1

找到一个问题2的解决方案(需要返回false),但我仍然不知道如何回答问题1.

用户按下我的应用程序中的按钮时,需要他们观看他们的视频.当他们第一次打开该屏幕时,我想让视频暂停,不能立即播放.我也想通过点击屏幕来暂停播放视频.当视频暂停时,我想显示ActionBar和MediaController.当视频恢复时,我想隐藏ActionBar和MediaController(可能稍稍延迟一段时间)?

我已经尝试了一些事情,但我最终遇到了问题,如视频将暂停但不能恢复,或者ActionBar和MediaController不会正常显示或隐藏.

更新2

我已经找到了问题1的部分解决方案,并更新了代码,以便在第一次打开时将视频显示为已暂停.但是,当它第一次打开时,它只显示一个黑色屏幕,直到我触摸视频来播放它.在观看视频一次后,将重新开始并暂停,等待再次播放,并从视频开始显示正确的图像.但是我一开始就不知道如何摆脱黑屏.

相关代码

public class ViewImageVideoFragment extends Fragment
{

    private int position = 0;
    private MediaController mMediaController;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mMediaController = new MediaController(getActivity());
        ...
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState)
    {

        if (savedInstanceState != null)
        {
            position = savedInstanceState.getInt("position");
        }

        View v = inflater.inflate(R.layout.fragment_video_view,parent,false);

        mVideoView = (VideoView) v.findViewById(R.id.fragmentVideoView);
        mVideoView.setVideoPath(videoPath);
        mVideoView.setMediaController(mMediaController);

        mVideoView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v,MotionEvent motionEvent)
            {
                if (mVideoView.isPlaying())
                {
                    mVideoView.pause();
                    if (!getActivity().getActionBar().isShowing())
                    {
                        getActivity().getActionBar().show();
                        mMediaController.show(0);
                    }
                    position = mVideoView.getCurrentPosition();
                    return false;
                }
                else
                {
                    if (getActivity().getActionBar().isShowing())
                    {
                        getActivity().getActionBar().hide();
                        mMediaController.hide();
                    }
                    mVideoView.seekTo(position);
                    mVideoView.start();
                    return false;
                }
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer)
            {
                mVideoView.seekTo(0);
            }
        });

        if (position != 0)
        {
            mVideoView.seekTo(position);
            mVideoView.start();
        }
        else
        {
             mVideoView.seekTo(0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);

        if (mVideoView != null)
        {
            savedInstanceState.putInt("position",mVideoView.getCurrentPosition());
        }

        mVideoView.pause();
    }
}

解决方法

要首先将视频暂停显示,只需将代码中的seekTo(0)更改为seekTo(1)即可.这将把视频移动到1毫秒的时间,你可以从那里拿走.
//edited here
private int position = 1;
private MediaController mMediaController;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    mMediaController = new MediaController(getActivity());
    ...
}

@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState)
{

    if (savedInstanceState != null)
    {
        position = savedInstanceState.getInt("position");
    }

    View v = inflater.inflate(R.layout.fragment_video_view,false);

    mVideoView = (VideoView) v.findViewById(R.id.fragmentVideoView);
    mVideoView.setVideoPath(videoPath);
    mVideoView.setMediaController(mMediaController);

    mVideoView.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v,MotionEvent motionEvent)
        {
            if (mVideoView.isPlaying())
            {
                mVideoView.pause();
                if (!getActivity().getActionBar().isShowing())
                {
                    getActivity().getActionBar().show();
                    mMediaController.show(0);
                }
                position = mVideoView.getCurrentPosition();
                return false;
            }
            else
            {
                if (getActivity().getActionBar().isShowing())
                {
                    getActivity().getActionBar().hide();
                    mMediaController.hide();
                }
                mVideoView.seekTo(position);
                mVideoView.start();
                return false;
            }
        }
    });

    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
    {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer)
        {
            //here
            mVideoView.seekTo(1);
        }
    });
    //here
    if (position != 1)
    {
        mVideoView.seekTo(position);
        mVideoView.start();
    }
    else
    {
         //here
         mVideoView.seekTo(1);
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);

    if (mVideoView != null)
    {
        savedInstanceState.putInt("position",mVideoView.getCurrentPosition());
    }

    mVideoView.pause();
}

}

猜你在找的Java相关文章