话不多说,先上图(本来是完整的图,不知道为啥显示这么一点点,大家还是下载下来看吧)
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onPrepared(MediaPlayer mp) {
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
videoView.setBackgroundColor(Color.TRANSPARENT);
return false;
}
});
}
});
注:仅支持4.4以上版本。
下面附上代码:
MainActivity.class
package com.bangni.videoplay;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
/**
* 使用video重复播放单条本地视频
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
private Button bt_next;
private String externalFilesDir;
public String path = "/storage/emulated/0/Android/data/com.bangni.tyy/files/video/1214018654434938880.mp4";
// /storage/emulated/0/Android/data/com.bangni.tyy/files/video/1218443884820303872.mp4
// /storage/emulated/0/Android/data/com.bangni.tyy/files/video/1214716732949889024.mp4
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoview);
bt_next = findViewById(R.id.bt_next);
bt_next.setOnClickListener(this);
videoView.setVideoURI(Uri.parse(path));
videoView.start();
// videoView.seekTo(0);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("TYY_CC_wjll", "我播完了");
// nextVideo();
videoView.setVideoURI(Uri.parse(path));
videoView.start();
}
});
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onPrepared(MediaPlayer mp) {
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
videoView.setBackgroundColor(Color.TRANSPARENT);
return false;
}
});
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
videoView.stopPlayback(); //播放异常,则停止播放,防止弹窗使界面阻塞
return true;
}
});
}
@Override
public void onClick(View v) {
int temdId = v.getId();
if(temdId == R.id.bt_next){
startActivity(new Intent(MainActivity.this, Main2Activity.class));
finish();
}
}
@Override
protected void onPause() {
super.onPause();
Log.d("TAG", "onPause-------------");
videoView.pause();
}
@Override
protected void onStop() {
super.onStop();
Log.d("TAG", "onStop-------------");
}
@Override
protected void onResume() {
super.onResume();
Log.d("TAG", "onResume-------------");
videoView.start();
}
}
注:这个是单视频重复播放的主要代码
Main2Activity.class:
package com.bangni.videoplay;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.List;
/**
* 多个本地视频重复播放
*/
public class Main2Activity extends AppCompatActivity {
private VideoView videoView;
private ImageView video_one;
private int posVideoIndex = 0;
private List datas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
videoView = findViewById(R.id.videoview2);
video_one = findViewById(R.id.video_one);
datas.add("/storage/emulated/0/Android/data/com.bangni.tyy/files/video/1214018654434938880.mp4");
datas.add("/storage/emulated/0/Android/data/com.bangni.tyy/files/video/1218443884820303872.mp4");
datas.add("/storage/emulated/0/Android/data/com.bangni.tyy/files/video/1214716732949889024.mp4");
startVideos();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("TYY_CC_wjll", "我播完了");
video_one.setVisibility(View.VISIBLE);
posVideoIndex++;
if(posVideoIndex >= datas.size()){
posVideoIndex = 0;
}
startVideos();
}
});
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onPrepared(MediaPlayer mp) {
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
video_one.setVisibility(View.GONE);
videoView.setBackgroundColor(Color.TRANSPARENT);
return false;
}
});
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
videoView.stopPlayback(); //播放异常,则停止播放,防止弹窗使界面阻塞
return true;
}
});
}
/**
* 播放视频
*/
private void startVideos() {
video_one.setImageBitmap(getVideoOne(datas.get(posVideoIndex)));
videoView.setVideoURI(Uri.parse(datas.get(posVideoIndex)));
videoView.start();
}
/**
* 获取视频的第一帧
* @param sdPath
* @return
*/
private Bitmap getVideoOne(String sdPath){
MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(sdPath);// videoPath 本地视频的路径
return media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC );
}
}
注:这个是多视频重复播放的主要代码;
还有一个小问题,VideoView设置宽高后并无效果谁怎么回事呢??
重写一下VideoView就好
代码如下:
package com.bangni.videoplay.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
// int width = wm.getDefaultDisplay().getWidth();
// int height = wm.getDefaultDisplay().getHeight();
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}
注:注释掉的打开就是全屏设置。不打开就是按照宽高设置的。
q:486789970
email:[email protected]
---财财亲笔