@@ -42,6 +42,10 @@ class SVGAParser(context: Context?) {
42
42
fun onError ()
43
43
}
44
44
45
+ interface PlayCallback {
46
+ fun onPlay (file : List <File >)
47
+ }
48
+
45
49
open class FileDownloader {
46
50
47
51
var noCache = false
@@ -130,7 +134,7 @@ class SVGAParser(context: Context?) {
130
134
mFrameHeight = frameHeight
131
135
}
132
136
133
- fun decodeFromAssets (name : String , callback : ParseCompletion ? ) {
137
+ fun decodeFromAssets (name : String , callback : ParseCompletion ? , playCallback : PlayCallback ? =null ) {
134
138
if (mContext == null ) {
135
139
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
136
140
return
@@ -139,15 +143,15 @@ class SVGAParser(context: Context?) {
139
143
LogUtils .info(TAG , " ================ decode from assets ================" )
140
144
threadPoolExecutor.execute {
141
145
mContext?.assets?.open(name)?.let {
142
- this .decodeFromInputStream(it, SVGACache .buildCacheKey(" file:///assets/$name " ), callback, true )
146
+ this .decodeFromInputStream(it, SVGACache .buildCacheKey(" file:///assets/$name " ), callback, true ,playCallback )
143
147
}
144
148
}
145
149
} catch (e: java.lang.Exception ) {
146
150
this .invokeErrorCallback(e, callback)
147
151
}
148
152
}
149
153
150
- fun decodeFromURL (url : URL , callback : ParseCompletion ? ): (() -> Unit )? {
154
+ fun decodeFromURL (url : URL , callback : ParseCompletion ? , playCallback : PlayCallback ? =null ): (() -> Unit )? {
151
155
if (mContext == null ) {
152
156
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
153
157
return null
@@ -160,17 +164,17 @@ class SVGAParser(context: Context?) {
160
164
if (SVGACache .isDefaultCache()) {
161
165
this .decodeFromCacheKey(cacheKey, callback)
162
166
} else {
163
- this ._decodeFromCacheKey (cacheKey, callback)
167
+ this ._decodeFromCacheKey (cacheKey, callback,playCallback )
164
168
}
165
169
}
166
170
return null
167
171
} else {
168
172
LogUtils .info(TAG , " no cached, prepare to download" )
169
173
fileDownloader.resume(url, {
170
174
if (SVGACache .isDefaultCache()) {
171
- this .decodeFromInputStream(it, cacheKey, callback)
175
+ this .decodeFromInputStream(it, cacheKey, callback, false ,playCallback )
172
176
} else {
173
- this ._decodeFromInputStream (it, cacheKey, callback)
177
+ this ._decodeFromInputStream (it, cacheKey, callback,playCallback )
174
178
}
175
179
}, {
176
180
this .invokeErrorCallback(it, callback)
@@ -181,7 +185,7 @@ class SVGAParser(context: Context?) {
181
185
/* *
182
186
* 读取解析本地缓存的svga文件.
183
187
*/
184
- fun _decodeFromCacheKey (cacheKey : String , callback : ParseCompletion ? ) {
188
+ fun _decodeFromCacheKey (cacheKey : String , callback : ParseCompletion ? , playCallback : PlayCallback ? ) {
185
189
val svga = SVGACache .buildSvgaFile(cacheKey)
186
190
try {
187
191
LogUtils .info(TAG , " cache.binary change to entity" )
@@ -197,10 +201,10 @@ class SVGAParser(context: Context?) {
197
201
mFrameWidth,
198
202
mFrameHeight
199
203
)
200
- videoItem.prepare {
204
+ videoItem.prepare( {
201
205
LogUtils .info(TAG , " cache.prepare success" )
202
206
this .invokeCompleteCallback(videoItem, callback)
203
- }
207
+ },playCallback)
204
208
} ? : doError(" cache.inflate(bytes) cause exception" , callback)
205
209
} ? : doError(" cache.readAsBytes(inputStream) cause exception" , callback)
206
210
} catch (e: Exception ) {
@@ -231,6 +235,7 @@ class SVGAParser(context: Context?) {
231
235
inputStream : InputStream ,
232
236
cacheKey : String ,
233
237
callback : ParseCompletion ?
238
+ ,playCallback : PlayCallback ?
234
239
) {
235
240
threadPoolExecutor.execute {
236
241
try {
@@ -261,10 +266,10 @@ class SVGAParser(context: Context?) {
261
266
// 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
262
267
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
263
268
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
264
- videoItem.prepare {
265
- LogUtils .info(TAG , " Input .prepare success" )
269
+ videoItem.prepare( {
270
+ LogUtils .info(TAG , " cache .prepare success" )
266
271
this .invokeCompleteCallback(videoItem, callback)
267
- }
272
+ },playCallback)
268
273
} ? : doError(" Input.inflate(bytes) cause exception" , callback)
269
274
} ? : doError(" Input.readAsBytes(inputStream) cause exception" , callback)
270
275
} catch (e: Exception ) {
@@ -279,7 +284,8 @@ class SVGAParser(context: Context?) {
279
284
inputStream : InputStream ,
280
285
cacheKey : String ,
281
286
callback : ParseCompletion ? ,
282
- closeInputStream : Boolean = false
287
+ closeInputStream : Boolean = false,
288
+ playCallback : PlayCallback ?
283
289
) {
284
290
if (mContext == null ) {
285
291
LogUtils .error(TAG , " 在配置 SVGAParser context 前, 无法解析 SVGA 文件。" )
@@ -314,10 +320,10 @@ class SVGAParser(context: Context?) {
314
320
mFrameWidth,
315
321
mFrameHeight
316
322
)
317
- videoItem.prepare {
318
- LogUtils .info(TAG , " decode from input stream, inflate end " )
323
+ videoItem.prepare( {
324
+ LogUtils .info(TAG , " cache.prepare success " )
319
325
this .invokeCompleteCallback(videoItem, callback)
320
- }
326
+ },playCallback)
321
327
322
328
} ? : this .invokeErrorCallback(
323
329
Exception (" inflate(bytes) cause exception" ),
@@ -343,23 +349,23 @@ class SVGAParser(context: Context?) {
343
349
*/
344
350
@Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromAssets(assetsName, callback)" ))
345
351
fun parse (assetsName : String , callback : ParseCompletion ? ) {
346
- this .decodeFromAssets(assetsName, callback)
352
+ this .decodeFromAssets(assetsName, callback, null )
347
353
}
348
354
349
355
/* *
350
356
* @deprecated from 2.4.0
351
357
*/
352
358
@Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromURL(url, callback)" ))
353
359
fun parse (url : URL , callback : ParseCompletion ? ) {
354
- this .decodeFromURL(url, callback)
360
+ this .decodeFromURL(url, callback, null )
355
361
}
356
362
357
363
/* *
358
364
* @deprecated from 2.4.0
359
365
*/
360
366
@Deprecated(" This method has been deprecated from 2.4.0." , ReplaceWith (" this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)" ))
361
367
fun parse (inputStream : InputStream , cacheKey : String , callback : ParseCompletion ? , closeInputStream : Boolean = false) {
362
- this .decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)
368
+ this .decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream, null )
363
369
}
364
370
365
371
private fun invokeCompleteCallback (videoItem : SVGAVideoEntity , callback : ParseCompletion ? ) {
@@ -394,6 +400,7 @@ class SVGAParser(context: Context?) {
394
400
LogUtils .info(TAG , " binary change to entity success" )
395
401
this .invokeCompleteCallback(SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
396
402
}
403
+
397
404
} catch (e: Exception ) {
398
405
LogUtils .error(TAG , " binary change to entity fail" , e)
399
406
cacheDir.delete()
0 commit comments