首先要解决的是播放资源文件视频的问题,很多第三方都不支持,所以这里直接用系统videoView写比较好。
本人找了相关assest读取播放的方法最后不得不放弃,支持Uri的就只有R.raw文件夹下的东西了。
代码如下:
视频的宽高适应不同手机不留缝隙问题。
继承VideoVideo重新测量即可解决,这里有人说很简单 网上还到处抄,还说这不需要什么算法,那么简单的话 你来。
String VIDEO_PATH = "android.resource://" + BuildConfig.APPLICATION_ID + "/" + R.raw.login;
loginActivityBinding.videoView.setDisplayAspectRatio(MeasureUtil.ASPECT_RATIO_PAVED_PARENT);
loginActivityBinding.videoView.setOnCorveHideListener(new SystemVideoView.OnCorveHideListener() {
@Override
public void requestHide() {
loginActivityBinding.corver.setVisibility(View.GONE);
}
});
loginActivityBinding.videoView.setVideoURI(Uri.parse(Constants.VIDEO_PATH));
loginActivityBinding.videoView.start();
loginActivityBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
loginActivityBinding.videoView.seekTo(0);
loginActivityBinding.videoView.start();
}
});//循环播放。
/**
* Created by qssq on 2017/7/10 qssq666@foxmail.com
*/
public class SystemVideoView extends VideoView {
private int videoWidth;//width
private int videoHeight;
private int displayAspectRatio;
public SystemVideoView(Context context) {
super(context);
}
public SystemVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SystemVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
// setVisibility(INVISIBLE);
}
protected void init(Context context) {
this.videoHeight = context.getResources().getDisplayMetrics().heightPixels;
this.videoWidth = context.getResources().getDisplayMetrics().widthPixels;
super.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
// setVisibility(VISIBLE);
SystemVideoView.this.videoWidth = mp.getVideoWidth();
SystemVideoView.this.videoHeight = mp.getVideoHeight();
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
if (onCorveHideListener != null) {
onCorveHideListener.requestHide();
}
}
if (onInfoListener != null) {
onInfoListener.onInfo(mp, what, extra);
}
return false;
}
});
}
});
}
MediaPlayer.OnPreparedListener onPreparedListener = null;
public interface OnCorveHideListener {
void requestHide();
}
@Override
public void setOnInfoListener(MediaPlayer.OnInfoListener onInfoListener) {
this.onInfoListener = onInfoListener;
}
MediaPlayer.OnInfoListener onInfoListener;
public void setOnCorveHideListener(OnCorveHideListener onCorveHideListener) {
this.onCorveHideListener = onCorveHideListener;
}
OnCorveHideListener onCorveHideListener;
@Override
public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
this.onPreparedListener = l;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/* int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int currentHeight = Math.max(height, videoHeight);
int currentWidth = currentHeight * 9 / 16;
setMeasuredDimension(currentWidth, currentHeight);*/
MeasureUtil.Size measure = MeasureUtil.measure(displayAspectRatio, widthMeasureSpec, heightMeasureSpec, videoWidth, videoHeight);
setMeasuredDimension(measure.width, measure.height);
}
public void setDisplayAspectRatio(int var1) {
displayAspectRatio = var1;
this.requestLayout();
}
@Override
public boolean isPlaying() {
return false;
}
public int getDisplayAspectRatio() {
return displayAspectRatio;
}
public void setCorver(int resource) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), resource, opts);
}
}