视频流优化

This commit is contained in:
张玉久
2020-01-14 11:47:40 +08:00
parent 61e3784991
commit 386923d16e
5 changed files with 101 additions and 62 deletions

View File

@ -22,19 +22,21 @@ import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class CacheManager {
private static final AtomicReference<CacheManager> INSTANCE = new AtomicReference<>();
private HashMap<String, Call> downCalls;
private volatile HashMap<String, Call> downCalls;
private OkHttpClient mClient;
private File cacheDirectory = StorageUtils.getIndividualCacheDirectory(HaloApp.getInstance().getApplication());
private FileNameGenerator generator = new Md5FileNameGenerator();
private final String TEMP_POSTFIX = ".download";
private final int preLength = 5 * 1024 * 1024;//预加载大小
public static CacheManager getInstance() {
for (; ; ) {
CacheManager current = INSTANCE.get();
@ -122,48 +124,58 @@ public class CacheManager {
@Override
public void subscribe(ObservableEmitter<CacheInfo> e) throws Exception {
String url = cacheInfo.getUrl();
long downloadLength = cacheInfo.getProgress();//已经下载好的长度
final long[] downloadLength = {cacheInfo.getProgress()};//已经下载好的长度
long contentLength = cacheInfo.getTotal();//文件的总长度
if (downloadLength >= preLength) {
if (downloadLength[0] >= preLength) {
e.onComplete();
return;
}
e.onNext(cacheInfo);
Request request = new Request.Builder()
.addHeader("RANGE", "bytes=" + downloadLength + "-" + (contentLength > preLength ? preLength : contentLength))
.addHeader("RANGE", "bytes=" + downloadLength[0] + "-" + (contentLength > preLength ? preLength : contentLength))
.url(url)
.build();
Call call = mClient.newCall(request);
downCalls.put(url, call);
Response response = call.execute();
File file = new File(cacheDirectory, cacheInfo.getFileName());
InputStream is = null;
FileOutputStream fileOutputStream = null;
try {
is = response.body().byteStream();
fileOutputStream = new FileOutputStream(file, true);
byte[] buffer = new byte[2048];
int len;
while (!call.isCanceled() && (len = is.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
downloadLength += len;
cacheInfo.setProgress(downloadLength);
e.onNext(cacheInfo);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
fileOutputStream.flush();
downCalls.remove(url);
} finally {
if (is != null) {
is.close();
@Override
public void onResponse(Call call, Response response) throws IOException {
File file = new File(cacheDirectory, cacheInfo.getFileName());
InputStream is = null;
FileOutputStream fileOutputStream = null;
try {
is = response.body().byteStream();
fileOutputStream = new FileOutputStream(file, true);
byte[] buffer = new byte[2048];
int len;
while (!call.isCanceled() && (len = is.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
downloadLength[0] += len;
cacheInfo.setProgress(downloadLength[0]);
e.onNext(cacheInfo);
}
fileOutputStream.flush();
downCalls.remove(url);
} finally {
if (is != null) {
is.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
if (file.length() == contentLength) {
file.renameTo(new File(file.getPath().substring(0, file.getPath().lastIndexOf(TEMP_POSTFIX))));
}
e.onComplete();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
if (file.length() == contentLength) {
file.renameTo(new File(file.getPath().substring(0, file.getPath().lastIndexOf(TEMP_POSTFIX))));
}
e.onComplete();
});
}
}