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

This commit is contained in:
zlzw 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.callback.FileCallback;
import com.lzy.okgo.model.Progress; import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response; 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; 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) { 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 @Override
public void onSuccess(Response<File> response) { public void onSuccess(Response<File> response) {
//下载成功结束后的回调 //下载成功结束后的回调
@ -78,7 +123,7 @@ public class DownloadUtil {
callback.onError(e); callback.onError(e);
} }
} }
}); });*/
} }

View File

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

View File

@ -2,7 +2,9 @@ package com.yunbao.live.views;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
@ -15,11 +17,16 @@ import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.alibaba.fastjson.JSON;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadTask;
import com.tencent.live2.V2TXLiveDef; import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer; import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePlayerObserver;
import com.tencent.live2.impl.V2TXLivePlayerImpl; import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.rtmp.ui.TXCloudVideoView; import com.tencent.rtmp.ui.TXCloudVideoView;
import com.yunbao.common.glide.ImgLoader; import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.http.CommonHttpConsts;
import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpCallback;
import com.yunbao.common.http.HttpClient; import com.yunbao.common.http.HttpClient;
import com.yunbao.common.utils.DialogUitl; import com.yunbao.common.utils.DialogUitl;
@ -37,6 +44,7 @@ import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import cn.rongcloud.rtc.api.RCRTCEngine; import cn.rongcloud.rtc.api.RCRTCEngine;
import cn.rongcloud.rtc.api.RCRTCRemoteUser; import cn.rongcloud.rtc.api.RCRTCRemoteUser;
@ -189,7 +197,71 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
if (TextUtils.isEmpty(url) || mVideoView == null) { if (TextUtils.isEmpty(url) || mVideoView == null) {
return; return;
} }
mPlayer.setCacheParams(1.0f, 5.0f);
mPlayer.setObserver(new V2TXLivePlayerObserver() {
String TAG = "播放流";
@Override
public void onError(V2TXLivePlayer player, int code, String msg, Bundle extraInfo) {
super.onError(player, code, msg, extraInfo);
Log.i(TAG, "onError: player = " + player + ", code = " + code + ", msg = " + msg + ", extraInfo = " + extraInfo);
}
@Override
public void onWarning(V2TXLivePlayer player, int code, String msg, Bundle extraInfo) {
super.onWarning(player, code, msg, extraInfo);
Log.i(TAG, "onWarning: " + "player = " + player + ", code = " + code + ", msg = " + msg + ", extraInfo = " + extraInfo);
if (code == 2105) {
// mPlayer.resumeVideo();
// mPlayer.resumeAudio();
/* mPlayer.stopPlay();
mPlayer.startPlay(purl);*/
}
}
@Override
public void onVideoPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle extraInfo) {
super.onVideoPlayStatusUpdate(player, status, reason, extraInfo);
//Log.i(TAG, "onVideoPlayStatusUpdate: " + "player = " + player + ", status = " + status + ", reason = " + reason + ", extraInfo = " + extraInfo);
}
@Override
public void onAudioPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle extraInfo) {
super.onAudioPlayStatusUpdate(player, status, reason, extraInfo);
//Log.i(TAG, "onAudioPlayStatusUpdate: " + "player = " + player + ", status = " + status + ", reason = " + reason + ", extraInfo = " + extraInfo);
}
@Override
public void onPlayoutVolumeUpdate(V2TXLivePlayer player, int volume) {
super.onPlayoutVolumeUpdate(player, volume);
}
@Override
public void onStatisticsUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayerStatistics statistics) {
super.onStatisticsUpdate(player, statistics);
if (statistics.fps == 0) {
OkDownload.getInstance().pauseAll();
} else {
OkDownload.getInstance().startAll();
}
}
@Override
public void onSnapshotComplete(V2TXLivePlayer player, Bitmap image) {
super.onSnapshotComplete(player, image);
}
@Override
public void onRenderVideoFrame(V2TXLivePlayer player, V2TXLiveDef.V2TXLiveVideoFrame videoFrame) {
super.onRenderVideoFrame(player, videoFrame);
}
@Override
public void onReceiveSeiMessage(V2TXLivePlayer player, int payloadType, byte[] data) {
super.onReceiveSeiMessage(player, payloadType, data);
}
});
mPlayer.setRenderView(mVideoView); mPlayer.setRenderView(mVideoView);
purl = url; purl = url;
mPlayer.startPlay(url); mPlayer.startPlay(url);
@ -216,7 +288,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
mCover.setVisibility(View.VISIBLE); mCover.setVisibility(View.VISIBLE);
} }
} }
if(mPlayer!=null) { if (mPlayer != null) {
mPlayer.stopPlay(); mPlayer.stopPlay();
} }
stopPlay2(); stopPlay2();