Skip to content

Commit 010b19c

Browse files
committed
feat: SVGASoundManager 增加音量控制
1 parent 1ac65c2 commit 010b19c

File tree

2 files changed

+67
-32
lines changed

2 files changed

+67
-32
lines changed

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

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ object SVGASoundManager {
2929

3030
private var soundPool: SoundPool? = null
3131

32-
private val completeCallBackMap: MutableMap<Int, CompleteCallBack> = mutableMapOf()
32+
private val soundCallBackMap: MutableMap<Int, SVGASoundCallBack> = mutableMapOf()
3333

34-
private object SingletonHolder{
35-
val holder = SVGASoundManager()
36-
}
37-
38-
companion object{
39-
fun get() = SingletonHolder.holder
40-
}
34+
/**
35+
* 音量设置,范围在 [0, 1] 之间
36+
*/
37+
private var volume: Float = 1f
4138

4239
/**
43-
* 音频加载完成回调
40+
* 音频回调
4441
*/
45-
interface CompleteCallBack {
42+
internal interface SVGASoundCallBack {
43+
44+
// 音量发生变化
45+
fun onVolumeChange(value: Float)
46+
47+
// 音频加载完成
4648
fun onComplete()
4749
}
4850

@@ -59,17 +61,51 @@ object SVGASoundManager {
5961
soundPool?.setOnLoadCompleteListener { _, soundId, status ->
6062
LogUtils.debug(TAG, "SoundPool onLoadComplete soundId=$soundId status=$status")
6163
if (status == 0) { //加载该声音成功
62-
if (completeCallBackMap.containsKey(soundId)) {
63-
completeCallBackMap[soundId]?.onComplete()
64+
if (soundCallBackMap.containsKey(soundId)) {
65+
soundCallBackMap[soundId]?.onComplete()
6466
}
6567
}
6668
}
6769
}
6870

6971
fun release() {
7072
LogUtils.debug(TAG, "**************** release ****************")
71-
if (completeCallBackMap.isNotEmpty()){
72-
completeCallBackMap.clear()
73+
if (soundCallBackMap.isNotEmpty()) {
74+
soundCallBackMap.clear()
75+
}
76+
}
77+
78+
/**
79+
* 根据当前播放实体,设置音量
80+
*
81+
* @param volume 范围在 [0, 1]
82+
* @param entity 根据需要控制对应 entity 音量大小,若为空则控制所有正在播放的音频音量
83+
*/
84+
fun setVolume(volume: Float, entity: SVGAVideoEntity? = null) {
85+
if (!checkInit()) {
86+
return
87+
}
88+
89+
if (volume < 0f || volume > 1f) {
90+
LogUtils.error(TAG, "The volume level is in the range of 0 to 1 ")
91+
return
92+
}
93+
94+
if (entity == null) {
95+
this.volume = volume
96+
val iterator = soundCallBackMap.entries.iterator()
97+
while (iterator.hasNext()) {
98+
val e = iterator.next()
99+
e.value.onVolumeChange(volume)
100+
}
101+
return
102+
}
103+
104+
val soundPool = soundPool ?: return
105+
106+
entity.audioList.forEach { audio ->
107+
val streamId = audio.playID ?: return
108+
soundPool.setVolume(streamId, volume, volume)
73109
}
74110
}
75111

@@ -101,19 +137,19 @@ object SVGASoundManager {
101137
SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0)
102138
}
103139

104-
fun load(callBack: CompleteCallBack?,
105-
fd: FileDescriptor?,
106-
offset: Long,
107-
length: Long,
108-
priority: Int): Int {
140+
internal fun load(callBack: SVGASoundCallBack?,
141+
fd: FileDescriptor?,
142+
offset: Long,
143+
length: Long,
144+
priority: Int): Int {
109145
if (!checkInit()) return -1
110146

111147
val soundId = soundPool!!.load(fd, offset, length, priority)
112148

113149
LogUtils.debug(TAG, "load soundId=$soundId callBack=$callBack")
114150

115-
if (callBack != null && !completeCallBackMap.containsKey(soundId)) {
116-
completeCallBackMap[soundId] = callBack
151+
if (callBack != null && !soundCallBackMap.containsKey(soundId)) {
152+
soundCallBackMap[soundId] = callBack
117153
}
118154
return soundId
119155
}
@@ -125,19 +161,14 @@ object SVGASoundManager {
125161

126162
soundPool!!.unload(soundId)
127163

128-
completeCallBackMap.remove(soundId)
164+
soundCallBackMap.remove(soundId)
129165
}
130166

131-
fun play(soundId: Int,
132-
leftVolume: Float,
133-
rightVolume: Float,
134-
priority: Int,
135-
loop: Int,
136-
rate: Float): Int {
167+
internal fun play(soundId: Int): Int {
137168
if (!checkInit()) return -1
138169

139170
LogUtils.debug(TAG, "play soundId=$soundId")
140-
return soundPool!!.play(soundId, leftVolume, rightVolume, priority, loop, rate)
171+
return soundPool!!.play(soundId, volume, volume, 1, 0, 1.0f)
141172
}
142173

143174
internal fun stop(soundId: Int) {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SVGAVideoEntity {
4343
internal var spriteList: List<SVGAVideoSpriteEntity> = emptyList()
4444
internal var audioList: List<SVGAAudioEntity> = emptyList()
4545
internal var soundPool: SoundPool? = null
46-
private var soundCallback: SVGASoundManager.CompleteCallBack? = null
46+
private var soundCallback: SVGASoundManager.SVGASoundCallBack? = null
4747
internal var imageMap = HashMap<String, Bitmap>()
4848
private var mCacheDir: File
4949
private var mFrameHeight = 0
@@ -286,8 +286,12 @@ class SVGAVideoEntity {
286286

287287
private fun setupSoundPool(entity: MovieEntity, completionBlock: () -> Unit) {
288288
var soundLoaded = 0
289-
if (SVGASoundManager.get().isInit()) {
290-
soundCallback = object : SVGASoundManager.CompleteCallBack {
289+
if (SVGASoundManager.isInit()) {
290+
soundCallback = object : SVGASoundManager.SVGASoundCallBack {
291+
override fun onVolumeChange(value: Float) {
292+
SVGASoundManager.setVolume(value, this@SVGAVideoEntity)
293+
}
294+
291295
override fun onComplete() {
292296
soundLoaded++
293297
if (soundLoaded >= entity.audios.count()) {

0 commit comments

Comments
 (0)