【android】解决VideoView设置循环播放不生效问题
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="1920px"
android:layout_height="720px"
tools:context=".MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="1920px"
android:layout_height="720px"
/>
</RelativeLayout>
Mainactivity
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
//获得下载目录,不推荐使用,api29已弃用,返回的是公共下载目录,需要声明存储权限
//File directory_doc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
//获得下载目录,返回的是应用下载目录
String file_path=getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+"/video";
Deposit(file_path,"video3.mp4");
//使用VideoView播放视频
VideoView videoView = (VideoView)findViewById(R.id.videoView);
String path=file_path+"/video3.mp4";
Log.d("播放地址",path);
videoView.setVideoPath(path);
// MediaPlayer mediaPlayer =new MediaPlayer();
// try {
// mediaPlayer.setDataSource(path);
// mediaPlayer.prepare();
// mediaPlayer.start();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
videoView.start();
// videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// @Override
// public void onPrepared(MediaPlayer mediaPlayer) {
// Log.d("yunlong", "onPrepared: ");
// mediaPlayer.setLooping(true);
// mediaPlayer.start();
//
// }
// });
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// 当播放完成时,重新开始播放
Log.d("yunlong", "onCompletion: ");
videoView.setVideoPath(path);
videoView.start();
}
});
// ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
// Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
// v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
// Log.d("播放地址","padding left"+systemBars.left+"top"+systemBars.top+"right"+systemBars.right+"bottom"+systemBars.bottom);
// return insets;
// });
}
//将assets内文件存储到本地下载目录
public Boolean Deposit(String path,String fileName){
InputStream inputStream;
try {
//判断文件是否存在
File file1=new File(path + "/" + fileName);
if(!file1.exists()){
inputStream=getAssets().open(fileName);
File file = new File(path);
//当目录不存在时创建目录
if(!file.exists()){
file.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + fileName);// 保存到本地的文件夹下的文件
byte[] buffer = new byte[1024];
int count = 0;
while ((count = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, count);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
}else {
Toast.makeText(MainActivity.this,"已存在",Toast.LENGTH_LONG).show();
}
return true;
}catch (IOException e){
e.printStackTrace();
}
return false;
}
}