implementation 'com.danikula:videocache:2.7.1'
public class MyApplication extends Application {
private HttpProxyCacheServer proxy;
public Vibrator mVibrator;
public static HttpProxyCacheServer getProxy() {
return getInstance().proxy == null ? (getInstance().proxy = getInstance().newProxy()) : getInstance().proxy;
}
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.cacheDirectory(getVedioFile())//缓存的文件夹,默认就不写
.maxCacheSize(512 * 1024 * 1024) // 512mb 最大大小,也可为最大个数
.build();
}
@Override
public void onCreate() {
super.onCreate();
}
public static File getVedioFile() {
String path = Environment.getExternalStorageDirectory().getPath() + "/cloudvideo";
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
return file;
}
}
//设置文件缓存可以是mp4或mp3及其他音视频文件
HttpProxyCacheServer proxy = MyApplication.getProxy();
String proxyUrl = proxy.getProxyUrl(“网络文件路径”);//videoUrl
//播放路径使用proxyUrl
MediaPlayer.setDataSource(proxyUrl );
|