我正在做一个基于视频视图的Android项目。我想在用户点击播放之前显示播放按钮,当用户决定暂停视频时 - 显示暂停按钮。单击暂停按钮应该会从暂停的同一位置(如YouTube视频)再次触发播放视频。
im1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vv1.setVideoURI(Uri.parse("android.resource://com.example.cm.filmfestival/" + R.raw.mission));
im1.setVisibility(View.INVISIBLE);
im2.setVisibility(View.INVISIBLE);
vv1.start();
}
});
im2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vv1.stopPlayback();
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
im1.setVisibility(View.VISIBLE);
vv1.start();
im2.setVisibility(View.VISIBLE);
vv1.stopPlayback();
return true;
}
答案 0 :(得分:2)
使用以下代码
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
public VideoView myVideoView;
private int position = 0;
private MediaController mediaControls;
// set the media controller buttons
if (mediaControls == null)
{
mediaControls = new MediaController(MainActivity.this);
}
// initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
try
{
// set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
// set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("your UrI"));
} catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
// we also set an setOnPreparedListener in order to know when the video
// file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mediaPlayer)
{
// if we have a position on savedInstanceState, the video
// playback should start from here
myVideoView.seekTo(position);
System.out.println("vidio is ready for playing");
if (position == 0)
{
myVideoView.start();
} else
{
// if we come from a resumed activity, video playback will
// be paused
myVideoView.pause();
}
}
});
答案 1 :(得分:0)
在VideoView上设置OntouchListener,然后在Ontouch回调中检查视频是否正在播放或暂停,然后再暂停或播放。
答案 2 :(得分:0)
...
<form action="{{ url_for('my_blueprint.my_login') }}" method="POST" name="login_user_form">
...
添加以上代码。
然后,添加private boolean stopped = false;
private ImageView postVideoPlaypauseIcon;
//set it to any play/pause icon
postVideoPlaypauseIcon = mView.findViewById(R.id.playpause_icon);
private int stopPosition;
类,让CustomVideoView
扩展它并创建一个对象VideoView
并使用video
findviewbyid
使用以下代码
替换VideoView的xmlpublic class CustomVideoView extends VideoView {
private PlayPauseListener mListener;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
@Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}
@Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}
public static interface PlayPauseListener {
void onPlay();
void onPause();
}
}
最后, 添加setOnTouchListener以侦听触摸。
`<package-name.CustomVideoView
android:id="@+id/custom_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>`
在您的videoView对象上添加video.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(stopped == false){
stopPosition = postVideo.getCurrentPosition();
video.pause();
stopped = true;
} else if(stopped == true){
video.seekTo(stopPosition);
video.start();
stopped = false;
}
Log.e("TAP","from video ");
return false;
}
});
setPlayPauseListener