Skip to content

Commit e03f08a

Browse files
committed
添加智能下载功能
1 parent 455eeb7 commit e03f08a

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

library/src/main/java/jaygoo/library/m3u8downloader/M3U8DownloadTask.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,12 @@ public void stop() {
338338
}
339339

340340
public File getM3u8File(String url){
341-
return new File(MUtils.getSaveFileDir(url), m3u8FileName);
341+
try {
342+
return new File(MUtils.getSaveFileDir(url), m3u8FileName);
343+
}catch (Exception e){
344+
M3U8Log.e(e.getMessage());
345+
}
346+
return null;
342347
}
343348

344349
}

library/src/main/java/jaygoo/library/m3u8downloader/M3U8Downloader.java

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,7 @@ private void pendingTask(M3U8Task task){
105105
}
106106
}
107107

108-
/**
109-
* 暂停,如果此任务正在下载则暂停,否则无反应
110-
* @param url
111-
*/
112-
public void pause(String url){
113-
if (TextUtils.isEmpty(url) || isQuicklyClick())return;
114-
m3U8DownLoadTask.stop();
115-
pauseList.add(url);
116-
currentM3U8Task.setState(M3U8TaskState.PAUSE);
117-
if (onM3U8DownloadListener != null){
118-
onM3U8DownloadListener.onDownloadPause(currentM3U8Task);
119-
}
120-
if (downLoadQueue.size() > 0 && url.equals(downLoadQueue.element())){
121-
downloadNextTask();
122-
}
123-
}
108+
124109

125110
/**
126111
* 取消任务
@@ -143,6 +128,23 @@ public void cancelAndDelete(String url){
143128
downloadNextTask();
144129
}
145130

131+
/**
132+
* 暂停,如果此任务正在下载则暂停,否则无反应
133+
* @param url
134+
*/
135+
public void pause(String url){
136+
if (TextUtils.isEmpty(url) || isQuicklyClick())return;
137+
m3U8DownLoadTask.stop();
138+
pauseList.add(url);
139+
currentM3U8Task.setState(M3U8TaskState.PAUSE);
140+
if (onM3U8DownloadListener != null){
141+
onM3U8DownloadListener.onDownloadPause(currentM3U8Task);
142+
}
143+
if (downLoadQueue.size() > 0 && url.equals(downLoadQueue.element())){
144+
downloadNextTask();
145+
}
146+
}
147+
146148
/**
147149
* 下载任务,如果当前任务在下载列表中则认为是插队,否则入队等候下载
148150
* @param url
@@ -159,13 +161,34 @@ public void download(String url){
159161
}
160162
}
161163

164+
/**
165+
* 智能下载
166+
* 如果文件已经下载完成则不再重复下载
167+
* 如果任务正在下载,则暂停当前任务
168+
* 否则加入下载队列
169+
* @param url
170+
*/
171+
public void smartDownload(String url){
172+
if (TextUtils.isEmpty(url) || isQuicklyClick() || checkM3U8IsExist(url))return;
173+
if (isTaskDownloading(url)){
174+
pause(url);
175+
}else {
176+
download(url);
177+
}
178+
}
179+
162180
/**
163181
* 检查m3u8文件是否存在
164182
* @param url
165183
* @return
166184
*/
167185
public boolean checkM3U8IsExist(String url){
168-
return m3U8DownLoadTask.getM3u8File(url).exists();
186+
try {
187+
return m3U8DownLoadTask.getM3u8File(url).exists();
188+
}catch (Exception e){
189+
M3U8Log.e(e.getMessage());
190+
}
191+
return false;
169192
}
170193

171194
/**
@@ -182,7 +205,7 @@ public boolean isRunning(){
182205
}
183206

184207
public List<String> getPauseList(){
185-
return pauseList;
208+
return pauseList;
186209
}
187210

188211
public boolean isTaskDownloading(String url){
@@ -221,7 +244,7 @@ private void startDownloadTask(String url){
221244

222245
@Override
223246
public void onDownloading(long totalFileSize, long itemFileSize, int totalTs, int curTs) {
224-
if (!m3U8DownLoadTask.isRunning())return;
247+
if (!m3U8DownLoadTask.isRunning())return;
225248
M3U8Log.d("onDownloading: "+totalFileSize+"|"+itemFileSize+"|"+totalTs+"|"+curTs);
226249
currentM3U8Task.setState(M3U8TaskState.DOWNLOADING);
227250
downloadProgress = 1.0f * curTs / totalTs;

0 commit comments

Comments
 (0)