VideoView onTouch事件:暂停/恢复video,以及显示/隐藏MediaController和ActionBar

问题摘要:

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

2)如何在触摸时暂停/取消暂停video,还隐藏/显示ActionBar和MediaController。

我很感激任何建议。 谢谢! (附有相关守则)

更新1

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

当用户按下我的应用程序中的按钮时,他们会观看他们的video。 当他们第一次打开屏幕时,我希望video暂停而不是立即播放。 我还希望能够通过点击屏幕暂停播放video。 当video暂停时,我想显示ActionBarMediaController 。 当video恢复时,我想隐藏ActionBarMediaController (可能会稍微延迟?)

我已经尝试了一些东西,但我最终遇到了问题,比如video会暂停但不能恢复,或者ActionBarMediaController无法正常显示或隐藏。

更新2

我find了问题1的部分解决方案,并更新了代码,以便在第一次打开时将video显示为暂停。 但是,当它第一次打开时,它只显示黑屏,直到我触摸video视图才能播放它。 观看video一次后,它将重置为开始和暂停,等待再次播放,并将从video的开头显示正确的图像。 但我不知道如何在开始时绕过黑屏。

相关代码:

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

要首先将video显示为暂停,只需在代码seekTo(0)更改为seekTo(1) 。 这会将video移动到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(); } 

}

如果我理解正确,您希望将video中的帧显示为占位符,直到您准备好开始播放video为止。 我知道有两种方法可以实现这一目标:

寻求

您可以使用MediaPlayer.seekTo将video向前移动一些帧,例如使用值150在video文件中以150毫秒显示帧。 video不需要启动才能进行搜索。

MediaMetadataRetriever

 MediaMetadataRetriever met = new MediaMetadataRetriever(); try { met.setDataSource(data[0], new HashMap()); //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); } 

这将为您提供一个Bitmap ,然后您可以将其放入ImageView并设置代替video。 但是,根据您正在处理的video编解码器的types,此API一直是我的错误。

我的消息 –

显示()
show(int timeout)
隐藏()
isShowing()
的onTouchEvent()

所有注释都在代码中

 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"); } 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(); /* Ok, so now you want to use that show(), preferrably without the int timeout I didn't add it in myself but you should be able to handle it yourself */ return true; } else /* I changed it to else, change it to if else if you have something specific you want to deal with */ { /* I would use that hide method I linked here, then start the video, I assume you know how to play the video yourself */ } return false; } }); mVideoView.seekTo(position); mVideoView.start(); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); if (mVideoView != null) { savedInstanceState.putInt("position", mVideoView.getCurrentPosition()); } mVideoView.pause(); } } 

我提供了其他方法,因为根据您的继续方式,他们可能会或可能不会预防未来的问题。