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

Commit 8437fd7

Browse files
author
zhusiliang
committed
feature:
1.新增文件回调,MP3文件可回调 commit info:
1 parent f29563e commit 8437fd7

File tree

7 files changed

+62
-29
lines changed

7 files changed

+62
-29
lines changed

app/src/main/assets/mp3_to_long.svga

310 KB
Binary file not shown.

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromAssetsActivity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
import com.opensource.svgaplayer.SVGAImageView;
1111
import com.opensource.svgaplayer.SVGAParser;
12+
import com.opensource.svgaplayer.SVGASoundManager;
1213
import com.opensource.svgaplayer.SVGAVideoEntity;
1314

1415
import org.jetbrains.annotations.NotNull;
1516

17+
import java.io.File;
1618
import java.util.ArrayList;
19+
import java.util.List;
1720

1821
public class AnimationFromAssetsActivity extends Activity {
1922

@@ -31,6 +34,7 @@ public void onClick(View view) {
3134
animationView.stepToFrame(currentIndex++, false);
3235
}
3336
});
37+
SVGASoundManager.Companion.get().init();
3438
loadAnimation();
3539
setContentView(animationView);
3640
}
@@ -39,21 +43,23 @@ private void loadAnimation() {
3943
SVGAParser svgaParser = SVGAParser.Companion.shareParser();
4044
// String name = this.randomSample();
4145
//asset jojo_audio.svga cannot callback
42-
String name = "jojo_audio.svga";
46+
String name = "mp3_to_long.svga";
4347
Log.d("SVGA", "## name " + name);
4448
svgaParser.setFrameSize(100, 100);
4549
svgaParser.decodeFromAssets(name, new SVGAParser.ParseCompletion() {
4650
@Override
4751
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
52+
Log.e("zzzz", "onComplete: ");
4853
animationView.setVideoItem(videoItem);
4954
animationView.stepToFrame(0, true);
5055
}
5156

5257
@Override
5358
public void onError() {
54-
59+
Log.e("zzzz", "onComplete: ");
5560
}
56-
});
61+
62+
}, null);
5763
}
5864

5965
private ArrayList<String> samples = new ArrayList();

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromClickActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
5353
public void onError() {
5454

5555
}
56-
});
56+
},null);
5757
}
5858

5959
}

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromNetworkActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.os.Bundle;
66
import android.support.annotation.Nullable;
77
import android.util.Log;
8-
import android.view.ViewGroup;
98

109
import com.opensource.svgaplayer.SVGAImageView;
1110
import com.opensource.svgaplayer.SVGAParser;
@@ -44,7 +43,9 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
4443
public void onError() {
4544

4645
}
47-
});
46+
47+
48+
},null);
4849
} catch (MalformedURLException e) {
4950
e.printStackTrace();
5051
}

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationWithDynamicImageActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.jetbrains.annotations.NotNull;
1515

16+
import java.io.File;
1617
import java.net.MalformedURLException;
1718
import java.net.URL;
1819

@@ -41,11 +42,12 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
4142
animationView.setImageDrawable(drawable);
4243
animationView.startAnimation();
4344
}
45+
4446
@Override
4547
public void onError() {
4648

4749
}
48-
});
50+
}, null);
4951
} catch (MalformedURLException e) {
5052
e.printStackTrace();
5153
}

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class SVGAParser(context: Context?) {
4242
fun onError()
4343
}
4444

45+
interface PlayCallback{
46+
fun onPlay(file: List<File>)
47+
}
48+
4549
open class FileDownloader {
4650

4751
var noCache = false
@@ -130,7 +134,7 @@ class SVGAParser(context: Context?) {
130134
mFrameHeight = frameHeight
131135
}
132136

133-
fun decodeFromAssets(name: String, callback: ParseCompletion?) {
137+
fun decodeFromAssets(name: String, callback: ParseCompletion?,playCallback: PlayCallback?=null) {
134138
if (mContext == null) {
135139
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
136140
return
@@ -139,15 +143,15 @@ class SVGAParser(context: Context?) {
139143
LogUtils.info(TAG, "================ decode from assets ================")
140144
threadPoolExecutor.execute {
141145
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)
143147
}
144148
}
145149
} catch (e: java.lang.Exception) {
146150
this.invokeErrorCallback(e, callback)
147151
}
148152
}
149153

150-
fun decodeFromURL(url: URL, callback: ParseCompletion?): (() -> Unit)? {
154+
fun decodeFromURL(url: URL, callback: ParseCompletion?,playCallback: PlayCallback?=null): (() -> Unit)? {
151155
if (mContext == null) {
152156
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
153157
return null
@@ -160,17 +164,17 @@ class SVGAParser(context: Context?) {
160164
if (SVGACache.isDefaultCache()) {
161165
this.decodeFromCacheKey(cacheKey, callback)
162166
} else {
163-
this._decodeFromCacheKey(cacheKey, callback)
167+
this._decodeFromCacheKey(cacheKey, callback,playCallback)
164168
}
165169
}
166170
return null
167171
} else {
168172
LogUtils.info(TAG, "no cached, prepare to download")
169173
fileDownloader.resume(url, {
170174
if (SVGACache.isDefaultCache()) {
171-
this.decodeFromInputStream(it, cacheKey, callback)
175+
this.decodeFromInputStream(it, cacheKey, callback,false,playCallback)
172176
} else {
173-
this._decodeFromInputStream(it, cacheKey, callback)
177+
this._decodeFromInputStream(it, cacheKey, callback,playCallback)
174178
}
175179
}, {
176180
this.invokeErrorCallback(it, callback)
@@ -181,7 +185,7 @@ class SVGAParser(context: Context?) {
181185
/**
182186
* 读取解析本地缓存的svga文件.
183187
*/
184-
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
188+
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?,playCallback: PlayCallback?) {
185189
val svga = SVGACache.buildSvgaFile(cacheKey)
186190
try {
187191
LogUtils.info(TAG, "cache.binary change to entity")
@@ -197,10 +201,10 @@ class SVGAParser(context: Context?) {
197201
mFrameWidth,
198202
mFrameHeight
199203
)
200-
videoItem.prepare {
204+
videoItem.prepare({
201205
LogUtils.info(TAG, "cache.prepare success")
202206
this.invokeCompleteCallback(videoItem, callback)
203-
}
207+
},playCallback)
204208
} ?: doError("cache.inflate(bytes) cause exception", callback)
205209
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback)
206210
} catch (e: Exception) {
@@ -231,6 +235,7 @@ class SVGAParser(context: Context?) {
231235
inputStream: InputStream,
232236
cacheKey: String,
233237
callback: ParseCompletion?
238+
,playCallback: PlayCallback?
234239
) {
235240
threadPoolExecutor.execute {
236241
try {
@@ -261,10 +266,10 @@ class SVGAParser(context: Context?) {
261266
// 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
262267
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
263268
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
264-
videoItem.prepare {
265-
LogUtils.info(TAG, "Input.prepare success")
269+
videoItem.prepare({
270+
LogUtils.info(TAG, "cache.prepare success")
266271
this.invokeCompleteCallback(videoItem, callback)
267-
}
272+
},playCallback)
268273
} ?: doError("Input.inflate(bytes) cause exception", callback)
269274
} ?: doError("Input.readAsBytes(inputStream) cause exception", callback)
270275
} catch (e: Exception) {
@@ -279,7 +284,8 @@ class SVGAParser(context: Context?) {
279284
inputStream: InputStream,
280285
cacheKey: String,
281286
callback: ParseCompletion?,
282-
closeInputStream: Boolean = false
287+
closeInputStream: Boolean = false,
288+
playCallback: PlayCallback?
283289
) {
284290
if (mContext == null) {
285291
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
@@ -314,10 +320,10 @@ class SVGAParser(context: Context?) {
314320
mFrameWidth,
315321
mFrameHeight
316322
)
317-
videoItem.prepare {
318-
LogUtils.info(TAG, "decode from input stream, inflate end")
323+
videoItem.prepare({
324+
LogUtils.info(TAG, "cache.prepare success")
319325
this.invokeCompleteCallback(videoItem, callback)
320-
}
326+
},playCallback)
321327

322328
} ?: this.invokeErrorCallback(
323329
Exception("inflate(bytes) cause exception"),
@@ -343,23 +349,23 @@ class SVGAParser(context: Context?) {
343349
*/
344350
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromAssets(assetsName, callback)"))
345351
fun parse(assetsName: String, callback: ParseCompletion?) {
346-
this.decodeFromAssets(assetsName, callback)
352+
this.decodeFromAssets(assetsName, callback,null)
347353
}
348354

349355
/**
350356
* @deprecated from 2.4.0
351357
*/
352358
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromURL(url, callback)"))
353359
fun parse(url: URL, callback: ParseCompletion?) {
354-
this.decodeFromURL(url, callback)
360+
this.decodeFromURL(url, callback,null)
355361
}
356362

357363
/**
358364
* @deprecated from 2.4.0
359365
*/
360366
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)"))
361367
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)
363369
}
364370

365371
private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
@@ -394,6 +400,7 @@ class SVGAParser(context: Context?) {
394400
LogUtils.info(TAG, "binary change to entity success")
395401
this.invokeCompleteCallback(SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
396402
}
403+
397404
} catch (e: Exception) {
398405
LogUtils.error(TAG, "binary change to entity fail", e)
399406
cacheDir.delete()

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.io.File
2121
import java.io.FileInputStream
2222
import java.io.FileOutputStream
2323
import java.util.*
24+
import kotlin.collections.ArrayList
2425

2526
/**
2627
* Created by PonyCui on 16/6/18.
@@ -47,6 +48,8 @@ class SVGAVideoEntity {
4748
private var mCacheDir: File
4849
private var mFrameHeight = 0
4950
private var mFrameWidth = 0
51+
private var mPlayCallback: SVGAParser.PlayCallback?=null
52+
private lateinit var mCallback: () -> Unit
5053

5154
constructor(json: JSONObject, cacheDir: File) : this(json, cacheDir, 0, 0)
5255

@@ -102,12 +105,14 @@ class SVGAVideoEntity {
102105
frames = movieParams.frames ?: 0
103106
}
104107

105-
internal fun prepare(callback: () -> Unit) {
108+
internal fun prepare(callback: () -> Unit,playCallback: SVGAParser.PlayCallback?) {
109+
mCallback=callback
110+
mPlayCallback=playCallback
106111
if (movieItem == null) {
107-
callback()
112+
mCallback()
108113
} else {
109114
setupAudios(movieItem!!) {
110-
callback()
115+
mCallback()
111116
}
112117
}
113118
}
@@ -195,6 +200,7 @@ class SVGAVideoEntity {
195200
//如果audiosFileMap为空 soundPool?.load 不会走 导致 setOnLoadCompleteListener 不会回调 导致外层prepare不回调卡住
196201
if(audiosFileMap.size==0 ){
197202
run(completionBlock)
203+
return
198204
}
199205
this.audioList = entity.audios.map { audio ->
200206
return@map createSvgaAudioEntity(audio, audiosFileMap)
@@ -209,7 +215,18 @@ class SVGAVideoEntity {
209215
// 除数不能为 0
210216
return item
211217
}
218+
//直接回调文件,后续播放都不走
219+
mPlayCallback?.let {
220+
val fileList:MutableList<File> =ArrayList()
221+
audiosFileMap.forEach{entity->
222+
fileList.add(entity.value)
223+
}
224+
it.onPlay(fileList)
225+
mCallback()
226+
return item
227+
}
212228
audiosFileMap[audio.audioKey]?.let { file ->
229+
213230
FileInputStream(file).use {
214231
val length = it.available().toDouble()
215232
val offset = ((startTime / totalTime) * length).toLong()

0 commit comments

Comments
 (0)