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

Commit

Permalink
feature:
Browse files Browse the repository at this point in the history
  1.新增文件回调,MP3文件可回调

commit info:
  • Loading branch information
zhusiliang committed Aug 3, 2021
1 parent f29563e commit 8437fd7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
Binary file added app/src/main/assets/mp3_to_long.svga
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGASoundManager;
import com.opensource.svgaplayer.SVGAVideoEntity;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AnimationFromAssetsActivity extends Activity {

Expand All @@ -31,6 +34,7 @@ public void onClick(View view) {
animationView.stepToFrame(currentIndex++, false);
}
});
SVGASoundManager.Companion.get().init();
loadAnimation();
setContentView(animationView);
}
Expand All @@ -39,21 +43,23 @@ private void loadAnimation() {
SVGAParser svgaParser = SVGAParser.Companion.shareParser();
// String name = this.randomSample();
//asset jojo_audio.svga cannot callback
String name = "jojo_audio.svga";
String name = "mp3_to_long.svga";
Log.d("SVGA", "## name " + name);
svgaParser.setFrameSize(100, 100);
svgaParser.decodeFromAssets(name, new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
Log.e("zzzz", "onComplete: ");
animationView.setVideoItem(videoItem);
animationView.stepToFrame(0, true);
}

@Override
public void onError() {

Log.e("zzzz", "onComplete: ");
}
});

}, null);
}

private ArrayList<String> samples = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
public void onError() {

}
});
},null);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ViewGroup;

import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
Expand Down Expand Up @@ -44,7 +43,9 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
public void onError() {

}
});


},null);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

Expand Down Expand Up @@ -41,11 +42,12 @@ public void onComplete(@NotNull SVGAVideoEntity videoItem) {
animationView.setImageDrawable(drawable);
animationView.startAnimation();
}

@Override
public void onError() {

}
});
}, null);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Expand Down
45 changes: 26 additions & 19 deletions library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class SVGAParser(context: Context?) {
fun onError()
}

interface PlayCallback{
fun onPlay(file: List<File>)
}

open class FileDownloader {

var noCache = false
Expand Down Expand Up @@ -130,7 +134,7 @@ class SVGAParser(context: Context?) {
mFrameHeight = frameHeight
}

fun decodeFromAssets(name: String, callback: ParseCompletion?) {
fun decodeFromAssets(name: String, callback: ParseCompletion?,playCallback: PlayCallback?=null) {
if (mContext == null) {
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
return
Expand All @@ -139,15 +143,15 @@ class SVGAParser(context: Context?) {
LogUtils.info(TAG, "================ decode from assets ================")
threadPoolExecutor.execute {
mContext?.assets?.open(name)?.let {
this.decodeFromInputStream(it, SVGACache.buildCacheKey("file:///assets/$name"), callback, true)
this.decodeFromInputStream(it, SVGACache.buildCacheKey("file:///assets/$name"), callback, true,playCallback)
}
}
} catch (e: java.lang.Exception) {
this.invokeErrorCallback(e, callback)
}
}

fun decodeFromURL(url: URL, callback: ParseCompletion?): (() -> Unit)? {
fun decodeFromURL(url: URL, callback: ParseCompletion?,playCallback: PlayCallback?=null): (() -> Unit)? {
if (mContext == null) {
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
return null
Expand All @@ -160,17 +164,17 @@ class SVGAParser(context: Context?) {
if (SVGACache.isDefaultCache()) {
this.decodeFromCacheKey(cacheKey, callback)
} else {
this._decodeFromCacheKey(cacheKey, callback)
this._decodeFromCacheKey(cacheKey, callback,playCallback)
}
}
return null
} else {
LogUtils.info(TAG, "no cached, prepare to download")
fileDownloader.resume(url, {
if (SVGACache.isDefaultCache()) {
this.decodeFromInputStream(it, cacheKey, callback)
this.decodeFromInputStream(it, cacheKey, callback,false,playCallback)
} else {
this._decodeFromInputStream(it, cacheKey, callback)
this._decodeFromInputStream(it, cacheKey, callback,playCallback)
}
}, {
this.invokeErrorCallback(it, callback)
Expand All @@ -181,7 +185,7 @@ class SVGAParser(context: Context?) {
/**
* 读取解析本地缓存的svga文件.
*/
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
fun _decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?,playCallback: PlayCallback?) {
val svga = SVGACache.buildSvgaFile(cacheKey)
try {
LogUtils.info(TAG, "cache.binary change to entity")
Expand All @@ -197,10 +201,10 @@ class SVGAParser(context: Context?) {
mFrameWidth,
mFrameHeight
)
videoItem.prepare {
videoItem.prepare({
LogUtils.info(TAG, "cache.prepare success")
this.invokeCompleteCallback(videoItem, callback)
}
},playCallback)
} ?: doError("cache.inflate(bytes) cause exception", callback)
} ?: doError("cache.readAsBytes(inputStream) cause exception", callback)
} catch (e: Exception) {
Expand Down Expand Up @@ -231,6 +235,7 @@ class SVGAParser(context: Context?) {
inputStream: InputStream,
cacheKey: String,
callback: ParseCompletion?
,playCallback: PlayCallback?
) {
threadPoolExecutor.execute {
try {
Expand Down Expand Up @@ -261,10 +266,10 @@ class SVGAParser(context: Context?) {
// 的svgaimageview处,把解析完的drawable或者entity缓存下来,下次直接播放.用完再调用clear()
// 在ImageView添加clearsAfterDetached,用于控制imageview在onDetach的时候是否要自动调用clear.
// 以暂时缓解需要为RecyclerView缓存drawable或者entity的人士.用完记得调用clear()
videoItem.prepare {
LogUtils.info(TAG, "Input.prepare success")
videoItem.prepare({
LogUtils.info(TAG, "cache.prepare success")
this.invokeCompleteCallback(videoItem, callback)
}
},playCallback)
} ?: doError("Input.inflate(bytes) cause exception", callback)
} ?: doError("Input.readAsBytes(inputStream) cause exception", callback)
} catch (e: Exception) {
Expand All @@ -279,7 +284,8 @@ class SVGAParser(context: Context?) {
inputStream: InputStream,
cacheKey: String,
callback: ParseCompletion?,
closeInputStream: Boolean = false
closeInputStream: Boolean = false,
playCallback: PlayCallback?
) {
if (mContext == null) {
LogUtils.error(TAG, "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
Expand Down Expand Up @@ -314,10 +320,10 @@ class SVGAParser(context: Context?) {
mFrameWidth,
mFrameHeight
)
videoItem.prepare {
LogUtils.info(TAG, "decode from input stream, inflate end")
videoItem.prepare({
LogUtils.info(TAG, "cache.prepare success")
this.invokeCompleteCallback(videoItem, callback)
}
},playCallback)

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

/**
* @deprecated from 2.4.0
*/
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromURL(url, callback)"))
fun parse(url: URL, callback: ParseCompletion?) {
this.decodeFromURL(url, callback)
this.decodeFromURL(url, callback,null)
}

/**
* @deprecated from 2.4.0
*/
@Deprecated("This method has been deprecated from 2.4.0.", ReplaceWith("this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)"))
fun parse(inputStream: InputStream, cacheKey: String, callback: ParseCompletion?, closeInputStream: Boolean = false) {
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream)
this.decodeFromInputStream(inputStream, cacheKey, callback, closeInputStream,null)
}

private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
Expand Down Expand Up @@ -394,6 +400,7 @@ class SVGAParser(context: Context?) {
LogUtils.info(TAG, "binary change to entity success")
this.invokeCompleteCallback(SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir, mFrameWidth, mFrameHeight), callback)
}

} catch (e: Exception) {
LogUtils.error(TAG, "binary change to entity fail", e)
cacheDir.delete()
Expand Down
23 changes: 20 additions & 3 deletions library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
import kotlin.collections.ArrayList

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

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

Expand Down Expand Up @@ -102,12 +105,14 @@ class SVGAVideoEntity {
frames = movieParams.frames ?: 0
}

internal fun prepare(callback: () -> Unit) {
internal fun prepare(callback: () -> Unit,playCallback: SVGAParser.PlayCallback?) {
mCallback=callback
mPlayCallback=playCallback
if (movieItem == null) {
callback()
mCallback()
} else {
setupAudios(movieItem!!) {
callback()
mCallback()
}
}
}
Expand Down Expand Up @@ -195,6 +200,7 @@ class SVGAVideoEntity {
//如果audiosFileMap为空 soundPool?.load 不会走 导致 setOnLoadCompleteListener 不会回调 导致外层prepare不回调卡住
if(audiosFileMap.size==0 ){
run(completionBlock)
return
}
this.audioList = entity.audios.map { audio ->
return@map createSvgaAudioEntity(audio, audiosFileMap)
Expand All @@ -209,7 +215,18 @@ class SVGAVideoEntity {
// 除数不能为 0
return item
}
//直接回调文件,后续播放都不走
mPlayCallback?.let {
val fileList:MutableList<File> =ArrayList()
audiosFileMap.forEach{entity->
fileList.add(entity.value)
}
it.onPlay(fileList)
mCallback()
return item
}
audiosFileMap[audio.audioKey]?.let { file ->

FileInputStream(file).use {
val length = it.available().toDouble()
val offset = ((startTime / totalTime) * length).toLong()
Expand Down

0 comments on commit 8437fd7

Please sign in to comment.