完善一下,加入一个视频是否需要循环播放
最近有一个播放视频的需求,要求很简单,就是打开Activity自动播放视频,然后监听下播放结束后显示某个图片或者跳转,于是动手封装一个
MediaVideoUtil
public class MediaVideoUtil {
private static MediaPlayer mediaPlayer;
/**
*
* @param context this
* @param surfaceView SurfaceView
* @param uri 文件地址
* String videoUrl= "android.resource://" + getPackageName() + "/" + R.raw.guo;
* Uri uri = Uri.parse(videoUrl);
* 网络地址
* Uri uri = Uri.parse("https://视频地址.mp4");
*/
public static void playerVideo(Context context,SurfaceView surfaceView, final Uri uri){
playerVideo(context,surfaceView,uri,false,null);
}
/**
*
* @param context this
* @param surfaceView SurfaceView
* @param uri 文件地址
* String videoUrl= "android.resource://" + getPackageName() + "/" + R.raw.guo;
* Uri uri = Uri.parse(videoUrl);
* 网络地址
* Uri uri = Uri.parse("https://视频地址.mp4");
* @param listener 播放结束之后操作
*/
public static void playerVideo(Context context,SurfaceView surfaceView, final Uri uri,boolean looping,final MediaPlayer.OnCompletionListener listener ){
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
mediaPlayer = new MediaPlayer();
//设置多媒体类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(context,uri);
mediaPlayer.prepare();
mediaPlayer.setDisplay(holder);
// 设置是否循环播放 ture:是 false:否
mediaPlayer.setLooping(looping);
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
//当SurfaceView的大小发生改变时候触发该方法
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
//Surface销毁时回掉
//当Surface销毁时候,同时把MediaPlayer也销毁
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
if (mediaPlayer!=null) {
mediaPlayer.stop();
//释放资源
mediaPlayer.release();
}
}
});
}
}
页面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
在MainActivity中的使用
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
//视频播放
String videoUrl= "android.resource://" + getPackageName() + "/" + R.raw.guo;
Uri uri = Uri.parse(videoUrl);
MediaVideoUtil.playerVideo(this, surfaceView, uri, true,new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(MainActivity.this, "视频播放完成!", Toast.LENGTH_SHORT).show();
}
});
}
对于小白来说不知道 "android.resource://" + getPackageName() + "/" + R.raw.guo;这是在哪里
给你们一个而贴图自己看啊
好了到此结束,已经够明白了啊