update:调整下载策略,如检测到直播播放器卡顿(fps为0)则暂停所有下载所有任务,当fps大于0则认为正常网络而继续下载,从而确保优先播放

This commit is contained in:
2022-10-18 16:45:49 +08:00
parent baa596158d
commit ec1656fbbd
3 changed files with 131 additions and 6 deletions

View File

@@ -4,6 +4,9 @@ import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadListener;
import java.io.File;
@@ -49,7 +52,49 @@ public class DownloadUtil {
}
public void download(String tag, final File fileDir, final String fileName, String url, final Callback callback) {
OkGo.<File>get(url).tag(tag).execute(new FileCallback(fileDir.getAbsolutePath(), fileName) {
//不用tag做tag是因为同一个tag下的下载任务都会被当一个任务处理可能导致状态混乱
//用url当tag则可以根据url来管理下载状态
DownloadListener downloadListener = new DownloadListener(url) {
@Override
public void onStart(Progress progress) {
}
@Override
public void onProgress(Progress progress) {
if (callback != null) {
int val = (int) (progress.currentSize * 100 / progress.totalSize);
// L.e("下载进度--->" + val);
callback.onProgress(val);
}
}
@Override
public void onError(Progress progress) {
L.e("下载失败--->" + progress.exception);
if (callback != null) {
callback.onError(progress.exception);
}
}
@Override
public void onFinish(File file, Progress progress) {
if (callback != null) {
callback.onSuccess(file);
}
OkDownload.getInstance().getTask(url).unRegister(this);
OkDownload.getInstance().removeTask(url);
}
@Override
public void onRemove(Progress progress) {
}
};
GetRequest<File> request = OkGo.<File>get(url);
OkDownload.request(url, request).fileName(fileName).folder(fileDir.getAbsolutePath()).register(downloadListener).save().start();
/*OkGo.<File>get(url).tag(tag).execute(new FileCallback(fileDir.getAbsolutePath(), fileName) {
@Override
public void onSuccess(Response<File> response) {
//下载成功结束后的回调
@@ -78,7 +123,7 @@ public class DownloadUtil {
callback.onError(e);
}
}
});
});*/
}

View File

@@ -2,6 +2,7 @@ package com.yunbao.common.utils;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.yunbao.common.CommonAppConfig;
import com.yunbao.common.Constants;
@@ -49,7 +50,10 @@ public class GiftCacheUtil {
if (!dir.exists()) {
dir.mkdirs();
}
File file1 = new File(dir, fileName + ".svga");
if(!fileName.contains(".svga")){
fileName+=".svga";
}
File file1 = new File(dir, fileName);
if (file1.exists()) {
commonCallback.callback(file1);
} else {
@@ -58,19 +62,23 @@ public class GiftCacheUtil {
ToastUtil.show("礼物正在获取中...");
}
downloadUtil.download(CommonHttpConsts.DOWNLOAD_GIF, dir, fileName, url, new DownloadUtil.Callback() {
String TAG="下载";
@Override
public void onSuccess(File file) {
Log.i(TAG, "onSuccess: 下载完成");
commonCallback.callback(file);
}
@Override
public void onProgress(int progress) {
Log.i(TAG, "onProgress: "+progress);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: "+e.getMessage());
commonCallback.callback(null);
e.printStackTrace();
}
});
}
@@ -232,7 +240,7 @@ public class GiftCacheUtil {
handler.post(() -> commonCallback.callback(null));
continue;
}
downloadUtil.download(CommonHttpConsts.DOWNLOAD_GIF, dir, Constants.GIF_GIFT_PREFIX + bean.getId(), bean.getSwf(), new DownloadUtil.Callback() {
downloadUtil.download(CommonHttpConsts.DOWNLOAD_GIF, dir, Constants.GIF_GIFT_PREFIX + bean.getId()+".svga", bean.getSwf(), new DownloadUtil.Callback() {
@Override
public void onSuccess(File file) {
downloadCache.remove(getIdForFileName(file.getName()));