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

Commit ffb1656

Browse files
authored
Merge pull request #380 from SakyaZsl/master
新增SoundPool单例,新增音频文件回调,修复其他若干bug
2 parents a171746 + b9cd0b1 commit ffb1656

File tree

12 files changed

+428
-290
lines changed

12 files changed

+428
-290
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
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;
14+
import com.opensource.svgaplayer.utils.log.SVGALogger;
1315

1416
import org.jetbrains.annotations.NotNull;
1517

18+
import java.io.File;
1619
import java.util.ArrayList;
20+
import java.util.List;
1721

1822
public class AnimationFromAssetsActivity extends Activity {
1923

@@ -31,6 +35,8 @@ public void onClick(View view) {
3135
animationView.stepToFrame(currentIndex++, false);
3236
}
3337
});
38+
SVGALogger.INSTANCE.setLogEnabled(true);
39+
SVGASoundManager.Companion.get().init();
3440
loadAnimation();
3541
setContentView(animationView);
3642
}
@@ -39,21 +45,23 @@ private void loadAnimation() {
3945
SVGAParser svgaParser = SVGAParser.Companion.shareParser();
4046
// String name = this.randomSample();
4147
//asset jojo_audio.svga cannot callback
42-
String name = "jojo_audio.svga";
48+
String name = "mp3_to_long.svga";
4349
Log.d("SVGA", "## name " + name);
4450
svgaParser.setFrameSize(100, 100);
4551
svgaParser.decodeFromAssets(name, new SVGAParser.ParseCompletion() {
4652
@Override
4753
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
54+
Log.e("zzzz", "onComplete: ");
4855
animationView.setVideoItem(videoItem);
4956
animationView.stepToFrame(0, true);
5057
}
5158

5259
@Override
5360
public void onError() {
54-
61+
Log.e("zzzz", "onComplete: ");
5562
}
56-
});
63+
64+
}, null);
5765
}
5866

5967
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/SVGACache.kt

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
package com.opensource.svgaplayer
22

33
import android.content.Context
4-
import com.opensource.svgaplayer.utils.log.LogUtils
54
import java.io.File
65
import java.net.URL
76
import java.security.MessageDigest
87

9-
/**
10-
* SVGA 缓存管理
11-
*/
12-
object SVGACache {
13-
14-
private const val TAG = "SVGACache"
158

9+
object SVGACache {
1610
enum class Type {
1711
DEFAULT,
1812
FILE
1913
}
2014

2115
private var type: Type = Type.DEFAULT
2216
private var cacheDir: String = "/"
23-
get() {
24-
if (field != "/") {
25-
val dir = File(field)
26-
if (!dir.exists()) {
27-
dir.mkdirs()
28-
}
29-
}
30-
return field
31-
}
3217

3318
fun onCreate(context: Context?) {
3419
onCreate(context, Type.DEFAULT)
@@ -42,52 +27,22 @@ object SVGACache {
4227
this.type = type
4328
}
4429

45-
/**
46-
* 清理缓存
47-
*/
48-
fun clearCache() {
49-
if (!isInitialized()) {
50-
LogUtils.error(TAG, "SVGACache is not init!")
51-
return
52-
}
53-
SVGAParser.threadPoolExecutor.execute {
54-
clearDir(cacheDir)
55-
LogUtils.info(TAG, "Clear svga cache done!")
56-
}
57-
}
58-
59-
// 清除目录下的所有文件
60-
private fun clearDir(path: String) {
61-
try {
62-
val dir = File(path)
63-
dir.takeIf { it.exists() }?.let { parentDir ->
64-
parentDir.listFiles()?.forEach { file ->
65-
if (!file.exists()) {
66-
return@forEach
67-
}
68-
if (file.isDirectory) {
69-
clearDir(file.absolutePath)
70-
}
71-
file.delete()
72-
}
73-
}
74-
} catch (e: Exception) {
75-
LogUtils.error(TAG, "Clear svga cache path: $path fail", e)
76-
}
77-
}
30+
// fun clearCache(context: Context?){
31+
// context ?: return
32+
// cacheDir = "${context.cacheDir.absolutePath}/svga/"
33+
// File(cacheDir).takeIf { it.exists() }?.delete()
34+
// }
7835

7936
fun isInitialized(): Boolean {
80-
return "/" != cacheDir
37+
return "/" != cacheDir&&File(cacheDir).exists()
8138
}
8239

8340
fun isDefaultCache(): Boolean = type == Type.DEFAULT
8441

8542
fun isCached(cacheKey: String): Boolean {
86-
return (if (isDefaultCache()) {
87-
buildCacheDir(cacheKey)
88-
} else {
89-
buildSvgaFile(cacheKey)
90-
}).exists()
43+
return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
44+
cacheKey
45+
)).exists()
9146
}
9247

9348
fun buildCacheKey(str: String): String {

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,47 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
5757
fun resume() {
5858
videoItem.audioList.forEach { audio ->
5959
audio.playID?.let {
60-
videoItem.soundPool?.resume(it)
60+
if (SVGASoundManager.get().isInit()){
61+
SVGASoundManager.get().resume(it)
62+
}else{
63+
videoItem.soundPool?.resume(it)
64+
}
6165
}
6266
}
6367
}
6468

6569
fun pause() {
6670
videoItem.audioList.forEach { audio ->
6771
audio.playID?.let {
68-
videoItem.soundPool?.pause(it)
72+
if (SVGASoundManager.get().isInit()){
73+
SVGASoundManager.get().pause(it)
74+
}else{
75+
videoItem.soundPool?.pause(it)
76+
}
6977
}
7078
}
7179
}
7280

7381
fun stop() {
7482
videoItem.audioList.forEach { audio ->
7583
audio.playID?.let {
76-
videoItem.soundPool?.stop(it)
84+
if (SVGASoundManager.get().isInit()){
85+
SVGASoundManager.get().stop(it)
86+
}else{
87+
videoItem.soundPool?.stop(it)
88+
}
7789
}
7890
}
7991
}
8092

8193
fun clear() {
8294
videoItem.audioList.forEach { audio ->
8395
audio.playID?.let {
84-
videoItem.soundPool?.stop(it)
96+
if (SVGASoundManager.get().isInit()){
97+
SVGASoundManager.get().stop(it)
98+
}else{
99+
videoItem.soundPool?.stop(it)
100+
}
85101
}
86102
audio.playID = null
87103
}

0 commit comments

Comments
 (0)