SimpleLameBridge.kt
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package uts.sdk.modules.uniRecorder
import android.util.Log
/**
* 运行时由 manifest plugins.audio.mp3 注入 SimpleLame;
* 编译期不 import、不打包 audio-mp3aac aar,避免 Dex 重复。
*/
object SimpleLameBridge {
private const val TAG = "SimpleLameBridge"
private const val CLASS_NAME = "io.dcloud.feature.audio.mp3.SimpleLame"
private var lameClass: Class<*>? = null
private fun clazz(): Class<*> {
if (lameClass == null) {
lameClass = Class.forName(CLASS_NAME)
}
return lameClass!!
}
fun init(inSampleRate: Int, channelCount: Int, outSampleRate: Int, bitrateKbps: Int, quality: Int) {
val method = clazz().getMethod(
"init",
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType
)
method.invoke(null, inSampleRate, channelCount, outSampleRate, bitrateKbps, quality)
}
fun encode(bufferL: ShortArray, bufferR: ShortArray, samples: Int, mp3buf: ByteArray): Int {
val method = clazz().getMethod(
"encode",
ShortArray::class.java,
ShortArray::class.java,
Int::class.javaPrimitiveType,
ByteArray::class.java
)
return method.invoke(null, bufferL, bufferR, samples, mp3buf) as Int
}
fun flush(mp3buf: ByteArray): Int {
val method = clazz().getMethod("flush", ByteArray::class.java)
return method.invoke(null, mp3buf) as Int
}
fun close() {
try {
val method = clazz().getMethod("close")
method.invoke(null)
} catch (e: Exception) {
Log.w(TAG, "SimpleLame.close failed: ${e.message}")
}
}
}