Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 39750fb

Browse files
committed
fix: crash occurs when playing non-existent files
1 parent dfacacb commit 39750fb

File tree

1 file changed

+96
-45
lines changed

1 file changed

+96
-45
lines changed

library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt

Lines changed: 96 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.Context
44
import android.net.http.HttpResponseCache
55
import android.os.Handler
66
import android.os.Looper
7-
import android.util.Log
87
import com.opensource.svgaplayer.proto.MovieEntity
98
import com.opensource.svgaplayer.utils.log.LogUtils
109
import org.json.JSONObject
@@ -46,7 +45,11 @@ class SVGAParser(context: Context?) {
4645

4746
var noCache = false
4847

49-
open fun resume(url: URL, complete: (inputStream: InputStream) -> Unit, failure: (e: Exception) -> Unit): () -> Unit {
48+
open fun resume(
49+
url: URL,
50+
complete: (inputStream: InputStream) -> Unit,
51+
failure: (e: Exception) -> Unit
52+
): () -> Unit {
5053
var cancelled = false
5154
val cancelBlock = {
5255
cancelled = true
@@ -55,8 +58,14 @@ class SVGAParser(context: Context?) {
5558
try {
5659
LogUtils.info(TAG, "================ svga file download start ================")
5760
if (HttpResponseCache.getInstalled() == null && !noCache) {
58-
LogUtils.error(TAG, "SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache")
59-
LogUtils.error(TAG, "在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache ")
61+
LogUtils.error(
62+
TAG,
63+
"SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache"
64+
)
65+
LogUtils.error(
66+
TAG,
67+
"在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache "
68+
)
6069
}
6170
(url.openConnection() as? HttpURLConnection)?.let {
6271
it.connectTimeout = 20 * 1000
@@ -68,7 +77,10 @@ class SVGAParser(context: Context?) {
6877
var count: Int
6978
while (true) {
7079
if (cancelled) {
71-
LogUtils.warn(TAG, "================ svga file download canceled ================")
80+
LogUtils.warn(
81+
TAG,
82+
"================ svga file download canceled ================"
83+
)
7284
break
7385
}
7486
count = inputStream.read(buffer, 0, 4096)
@@ -78,11 +90,17 @@ class SVGAParser(context: Context?) {
7890
outputStream.write(buffer, 0, count)
7991
}
8092
if (cancelled) {
81-
LogUtils.warn(TAG, "================ svga file download canceled ================")
93+
LogUtils.warn(
94+
TAG,
95+
"================ svga file download canceled ================"
96+
)
8297
return@execute
8398
}
8499
ByteArrayInputStream(outputStream.toByteArray()).use {
85-
LogUtils.info(TAG, "================ svga file download complete ================")
100+
LogUtils.info(
101+
TAG,
102+
"================ svga file download complete ================"
103+
)
86104
complete(it)
87105
}
88106
}
@@ -135,15 +153,20 @@ class SVGAParser(context: Context?) {
135153
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
136154
return
137155
}
138-
try {
139-
LogUtils.info(TAG, "================ decode from assets ================")
140-
threadPoolExecutor.execute {
156+
LogUtils.info(TAG, "================ decode from assets ================")
157+
threadPoolExecutor.execute {
158+
try {
141159
mContext?.assets?.open(name)?.let {
142-
this.decodeFromInputStream(it, SVGACache.buildCacheKey("file:///assets/$name"), callback, true)
160+
this.decodeFromInputStream(
161+
it,
162+
SVGACache.buildCacheKey("file:///assets/$name"),
163+
callback,
164+
true
165+
)
143166
}
167+
} catch (e: java.lang.Exception) {
168+
this.invokeErrorCallback(e, callback)
144169
}
145-
} catch (e: java.lang.Exception) {
146-
this.invokeErrorCallback(e, callback)
147170
}
148171
}
149172

@@ -192,10 +215,10 @@ class SVGAParser(context: Context?) {
192215
inflate(bytes)?.let { inflateBytes ->
193216
LogUtils.info(TAG, "cache.inflate success")
194217
val videoItem = SVGAVideoEntity(
195-
MovieEntity.ADAPTER.decode(inflateBytes),
196-
File(cacheKey),
197-
mFrameWidth,
198-
mFrameHeight
218+
MovieEntity.ADAPTER.decode(inflateBytes),
219+
File(cacheKey),
220+
mFrameWidth,
221+
mFrameHeight
199222
)
200223
videoItem.prepare {
201224
LogUtils.info(TAG, "cache.prepare success")
@@ -219,18 +242,18 @@ class SVGAParser(context: Context?) {
219242
fun doError(error: String, callback: ParseCompletion?) {
220243
LogUtils.info(TAG, error)
221244
this.invokeErrorCallback(
222-
Exception(error),
223-
callback
245+
Exception(error),
246+
callback
224247
)
225248
}
226249

227250
/**
228251
* 读取解析来自URL的svga文件.并缓存成本地文件
229252
*/
230253
fun _decodeFromInputStream(
231-
inputStream: InputStream,
232-
cacheKey: String,
233-
callback: ParseCompletion?
254+
inputStream: InputStream,
255+
cacheKey: String,
256+
callback: ParseCompletion?
234257
) {
235258
threadPoolExecutor.execute {
236259
try {
@@ -251,10 +274,10 @@ class SVGAParser(context: Context?) {
251274
inflate(bytes)?.let { inflateBytes ->
252275
LogUtils.info(TAG, "Input.inflate success")
253276
val videoItem = SVGAVideoEntity(
254-
MovieEntity.ADAPTER.decode(inflateBytes),
255-
File(cacheKey),
256-
mFrameWidth,
257-
mFrameHeight
277+
MovieEntity.ADAPTER.decode(inflateBytes),
278+
File(cacheKey),
279+
mFrameWidth,
280+
mFrameHeight
258281
)
259282
// 里面soundPool如果解析时load同一个svga的声音文件会出现无回调的情况,导致这里的callback不执行,
260283
// 原因暂时未知.目前解决方案是公开imageview,drawable,entity的clear(),然后在播放带声音
@@ -276,10 +299,10 @@ class SVGAParser(context: Context?) {
276299
}
277300

278301
fun decodeFromInputStream(
279-
inputStream: InputStream,
280-
cacheKey: String,
281-
callback: ParseCompletion?,
282-
closeInputStream: Boolean = false
302+
inputStream: InputStream,
303+
cacheKey: String,
304+
callback: ParseCompletion?,
305+
closeInputStream: Boolean = false
283306
) {
284307
if (mContext == null) {
285308
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
@@ -309,24 +332,24 @@ class SVGAParser(context: Context?) {
309332
LogUtils.info(TAG, "decode from input stream, inflate start")
310333
inflate(bytes)?.let {
311334
val videoItem = SVGAVideoEntity(
312-
MovieEntity.ADAPTER.decode(it),
313-
File(cacheKey),
314-
mFrameWidth,
315-
mFrameHeight
335+
MovieEntity.ADAPTER.decode(it),
336+
File(cacheKey),
337+
mFrameWidth,
338+
mFrameHeight
316339
)
317340
videoItem.prepare {
318341
LogUtils.info(TAG, "decode from input stream, inflate end")
319342
this.invokeCompleteCallback(videoItem, callback)
320343
}
321344

322345
} ?: this.invokeErrorCallback(
323-
Exception("inflate(bytes) cause exception"),
324-
callback
346+
Exception("inflate(bytes) cause exception"),
347+
callback
325348
)
326349
}
327350
} ?: this.invokeErrorCallback(
328-
Exception("readAsBytes(inputStream) cause exception"),
329-
callback
351+
Exception("readAsBytes(inputStream) cause exception"),
352+
callback
330353
)
331354
} catch (e: java.lang.Exception) {
332355
this.invokeErrorCallback(e, callback)
@@ -341,24 +364,38 @@ class SVGAParser(context: Context?) {
341364
/**
342365
* @deprecated from 2.4.0
343366
*/
344-
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromAssets(assetsName, callback)"))
367+
@Deprecated(
368+
"This method has been deprecated from 2.4.0.",
369+
ReplaceWith("this.decodeFromAssets(assetsName, callback)")
370+
)
345371
fun parse(assetsName: String, callback: ParseCompletion?) {
346372
this.decodeFromAssets(assetsName, callback)
347373
}
348374

349375
/**
350376
* @deprecated from 2.4.0
351377
*/
352-
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromURL(url, callback)"))
378+
@Deprecated(
379+
"This method has been deprecated from 2.4.0.",
380+
ReplaceWith("this.decodeFromURL(url, callback)")
381+
)
353382
fun parse(url: URL, callback: ParseCompletion?) {
354383
this.decodeFromURL(url, callback)
355384
}
356385

357386
/**
358387
* @deprecated from 2.4.0
359388
*/
360-
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)"))
361-
fun parse(inputStream: InputStream, cacheKey: String, callback: ParseCompletion?, closeInputStream: Boolean = false) {
389+
@Deprecated(
390+
"This method has been deprecated from 2.4.0.",
391+
ReplaceWith("this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)")
392+
)
393+
fun parse(
394+
inputStream: InputStream,
395+
cacheKey: String,
396+
callback: ParseCompletion?,
397+
closeInputStream: Boolean = false
398+
) {
362399
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)
363400
}
364401

@@ -392,7 +429,14 @@ class SVGAParser(context: Context?) {
392429
LogUtils.info(TAG, "binary change to entity")
393430
FileInputStream(binaryFile).use {
394431
LogUtils.info(TAG, "binary change to entity success")
395-
this.invokeCompleteCallback(SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
432+
this.invokeCompleteCallback(
433+
SVGAVideoEntity(
434+
MovieEntity.ADAPTER.decode(it),
435+
cacheDir,
436+
mFrameWidth,
437+
mFrameHeight
438+
), callback
439+
)
396440
}
397441
} catch (e: Exception) {
398442
LogUtils.error(TAG, "binary change to entity fail", e)
@@ -417,7 +461,14 @@ class SVGAParser(context: Context?) {
417461
byteArrayOutputStream.toString().let {
418462
JSONObject(it).let {
419463
LogUtils.info(TAG, "spec change to entity success")
420-
this.invokeCompleteCallback(SVGAVideoEntity(it, cacheDir, mFrameWidth, mFrameHeight), callback)
464+
this.invokeCompleteCallback(
465+
SVGAVideoEntity(
466+
it,
467+
cacheDir,
468+
mFrameWidth,
469+
mFrameHeight
470+
), callback
471+
)
421472
}
422473
}
423474
}
@@ -494,7 +545,7 @@ class SVGAParser(context: Context?) {
494545
fileOutputStream.write(buff, 0, readBytes)
495546
}
496547
}
497-
LogUtils.error(TAG, "================ unzip complete ================")
548+
LogUtils.info(TAG, "================ unzip complete ================")
498549
zipInputStream.closeEntry()
499550
}
500551
}

0 commit comments

Comments
 (0)