视频查看触摸事件:暂停/恢复视频,以及显示/隐藏媒体控制器和操作栏

2022-09-04 08:45:17

问题摘要:

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

2)如何在触摸时暂停/取消暂停视频,以及隐藏/显示动作栏和媒体控制器。

我将不胜感激任何建议。谢谢!(附相关条文)

更新 1

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

当用户在我的应用中按下按钮时,他们需要观看他们的视频。当他们第一次打开该屏幕时,我希望视频暂停并且不会立即播放。我还希望能够通过点击屏幕来暂停视频的播放。当视频暂停时,我想显示 和 .当视频恢复时,我想隐藏和(可能在稍有延迟之后?ActionBarMediaControllerActionBarMediaController

我已经尝试了一些事情,但我最终遇到了问题,例如视频将暂停但无法恢复,或者并且无法正确显示或隐藏。ActionBarMediaController

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

答案 1

若要首先将视频显示为已暂停,只需在代码中更改为 。这会将视频移动到1毫秒的时间,您可以从那里获取它。seekTo(0)seekTo(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, 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)
        {
            //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();
}

}


答案 2

如果我理解正确,您希望将视频中的帧显示为占位符,直到您准备好开始视频为止。我知道有两种方法可以做到这一点:

寻求

您可以使用 将视频向前移动几帧,例如,使用该值在视频文件中的第 150 毫秒处显示帧。无需启动视频即可进行搜索。MediaPlayer.seekTo150

MediaMetadataRetriever

MediaMetadataRetriever met = new MediaMetadataRetriever();

try { 
    met.setDataSource(data[0], new HashMap<String, String>()); //use this constructor, other one has a bug...
    Bitmap b = met.getFrameAtTime();

    if (b == null)
        b = met.getFrameAtTime(150, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);

    met.release();
    return b;

} catch (Exception e) {
    Log.d(TAG, "MediaMetadata failed", e);
}

这将为您提供一个,然后您可以将其放入并设置以代替视频。但是,这个API对我来说一直有问题,具体取决于您正在处理的视频编解码器的类型。BitmapImageView


推荐