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

Commit

Permalink
fix bug:
Browse files Browse the repository at this point in the history
  1.兼容48khz的音频
  2.修复音频错误没有回调
  3.修复清除缓存之后,不弹出svga

commit info:
  • Loading branch information
zhusiliang committed Aug 2, 2021
1 parent fc2d35e commit f29563e
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 269 deletions.
65 changes: 10 additions & 55 deletions library/src/main/java/com/opensource/svgaplayer/SVGACache.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
package com.opensource.svgaplayer

import android.content.Context
import com.opensource.svgaplayer.utils.log.LogUtils
import java.io.File
import java.net.URL
import java.security.MessageDigest

/**
* SVGA 缓存管理
*/
object SVGACache {

private const val TAG = "SVGACache"

object SVGACache {
enum class Type {
DEFAULT,
FILE
}

private var type: Type = Type.DEFAULT
private var cacheDir: String = "/"
get() {
if (field != "/") {
val dir = File(field)
if (!dir.exists()) {
dir.mkdirs()
}
}
return field
}

fun onCreate(context: Context?) {
onCreate(context, Type.DEFAULT)
Expand All @@ -42,52 +27,22 @@ object SVGACache {
this.type = type
}

/**
* 清理缓存
*/
fun clearCache() {
if (!isInitialized()) {
LogUtils.error(TAG, "SVGACache is not init!")
return
}
SVGAParser.threadPoolExecutor.execute {
clearDir(cacheDir)
LogUtils.info(TAG, "Clear svga cache done!")
}
}

// 清除目录下的所有文件
private fun clearDir(path: String) {
try {
val dir = File(path)
dir.takeIf { it.exists() }?.let { parentDir ->
parentDir.listFiles()?.forEach { file ->
if (!file.exists()) {
return@forEach
}
if (file.isDirectory) {
clearDir(file.absolutePath)
}
file.delete()
}
}
} catch (e: Exception) {
LogUtils.error(TAG, "Clear svga cache path: $path fail", e)
}
}
// fun clearCache(context: Context?){
// context ?: return
// cacheDir = "${context.cacheDir.absolutePath}/svga/"
// File(cacheDir).takeIf { it.exists() }?.delete()
// }

fun isInitialized(): Boolean {
return "/" != cacheDir
return "/" != cacheDir&&File(cacheDir).exists()
}

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

fun isCached(cacheKey: String): Boolean {
return (if (isDefaultCache()) {
buildCacheDir(cacheKey)
} else {
buildSvgaFile(cacheKey)
}).exists()
return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
cacheKey
)).exists()
}

fun buildCacheKey(str: String): String {
Expand Down
24 changes: 20 additions & 4 deletions library/src/main/java/com/opensource/svgaplayer/SVGADrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,47 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
fun resume() {
videoItem.audioList.forEach { audio ->
audio.playID?.let {
videoItem.soundPool?.resume(it)
if (SVGASoundManager.get().isInit()){
SVGASoundManager.get().resume(it)
}else{
videoItem.soundPool?.resume(it)
}
}
}
}

fun pause() {
videoItem.audioList.forEach { audio ->
audio.playID?.let {
videoItem.soundPool?.pause(it)
if (SVGASoundManager.get().isInit()){
SVGASoundManager.get().pause(it)
}else{
videoItem.soundPool?.pause(it)
}
}
}
}

fun stop() {
videoItem.audioList.forEach { audio ->
audio.playID?.let {
videoItem.soundPool?.stop(it)
if (SVGASoundManager.get().isInit()){
SVGASoundManager.get().stop(it)
}else{
videoItem.soundPool?.stop(it)
}
}
}
}

fun clear() {
videoItem.audioList.forEach { audio ->
audio.playID?.let {
videoItem.soundPool?.stop(it)
if (SVGASoundManager.get().isInit()){
SVGASoundManager.get().stop(it)
}else{
videoItem.soundPool?.stop(it)
}
}
audio.playID = null
}
Expand Down
Loading

0 comments on commit f29563e

Please sign in to comment.