@@ -29,20 +29,22 @@ object SVGASoundManager {
29
29
30
30
private var soundPool: SoundPool ? = null
31
31
32
- private val completeCallBackMap : MutableMap <Int , CompleteCallBack > = mutableMapOf ()
32
+ private val soundCallBackMap : MutableMap <Int , SVGASoundCallBack > = mutableMapOf ()
33
33
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
41
38
42
39
/* *
43
- * 音频加载完成回调
40
+ * 音频回调
44
41
*/
45
- interface CompleteCallBack {
42
+ internal interface SVGASoundCallBack {
43
+
44
+ // 音量发生变化
45
+ fun onVolumeChange (value : Float )
46
+
47
+ // 音频加载完成
46
48
fun onComplete ()
47
49
}
48
50
@@ -59,17 +61,51 @@ object SVGASoundManager {
59
61
soundPool?.setOnLoadCompleteListener { _, soundId, status ->
60
62
LogUtils .debug(TAG , " SoundPool onLoadComplete soundId=$soundId status=$status " )
61
63
if (status == 0 ) { // 加载该声音成功
62
- if (completeCallBackMap .containsKey(soundId)) {
63
- completeCallBackMap [soundId]?.onComplete()
64
+ if (soundCallBackMap .containsKey(soundId)) {
65
+ soundCallBackMap [soundId]?.onComplete()
64
66
}
65
67
}
66
68
}
67
69
}
68
70
69
71
fun release () {
70
72
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)
73
109
}
74
110
}
75
111
@@ -101,19 +137,19 @@ object SVGASoundManager {
101
137
SoundPool (maxStreams, AudioManager .STREAM_MUSIC , 0 )
102
138
}
103
139
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 {
109
145
if (! checkInit()) return - 1
110
146
111
147
val soundId = soundPool!! .load(fd, offset, length, priority)
112
148
113
149
LogUtils .debug(TAG , " load soundId=$soundId callBack=$callBack " )
114
150
115
- if (callBack != null && ! completeCallBackMap .containsKey(soundId)) {
116
- completeCallBackMap [soundId] = callBack
151
+ if (callBack != null && ! soundCallBackMap .containsKey(soundId)) {
152
+ soundCallBackMap [soundId] = callBack
117
153
}
118
154
return soundId
119
155
}
@@ -125,19 +161,14 @@ object SVGASoundManager {
125
161
126
162
soundPool!! .unload(soundId)
127
163
128
- completeCallBackMap .remove(soundId)
164
+ soundCallBackMap .remove(soundId)
129
165
}
130
166
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 {
137
168
if (! checkInit()) return - 1
138
169
139
170
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 )
141
172
}
142
173
143
174
internal fun stop (soundId : Int ) {
0 commit comments