refactor: 优化音频录制相关函数,简化代码结构,增强错误处理逻辑

This commit is contained in:
fanpeng 2025-07-10 18:43:35 +08:00
parent 6d538569f7
commit ffe0cada1c

View File

@ -42,9 +42,9 @@ class MyFLVListener extends FLVListener {
}
function addADTStoPacket(packet: ByteArray, packetLen: Int) {
const profile = 2 // AAC LC
const freqIdx = 8 // 16000Hz
const chanCfg = 1 // MONO
const profile = 2
const freqIdx = 8
const chanCfg = 1
packet[0] = (0xff).toByte()
packet[1] = (0xf9).toByte()
@ -58,40 +58,23 @@ function addADTStoPacket(packet: ByteArray, packetLen: Int) {
export const requestPermission = async function (): Promise<Result> {
return new Promise(resolve => {
try {
let permissionNeed = ['android.permission.RECORD_AUDIO']
const permissionNeed = ['android.permission.RECORD_AUDIO']
UTSAndroid.requestSystemPermission(
UTSAndroid.getUniActivity()!,
permissionNeed,
function (allRight: boolean, _: string[]) {
(allRight: boolean, _: string[]) => {
if (allRight) {
resolve({
code: 0,
data: {},
message: '成功'
})
resolve({ code: 0, data: {}, message: '成功' })
} else {
resolve({
code: -1,
data: {},
message: '失败'
})
resolve({ code: -1, data: {}, message: '失败' })
}
},
function (_: boolean, _: string[]) {
resolve({
code: -1,
data: {},
message: '失败'
})
(_: boolean, _: string[]) => {
resolve({ code: -1, data: {}, message: '失败' })
}
)
} catch (error) {
resolve({
code: -1,
data: {},
message: error.toString()
})
resolve({ code: -1, data: {}, message: error.toString() })
}
})
}
@ -117,39 +100,21 @@ export const initAudio = async function (): Promise<Result> {
bufferSizeInBytes
)
const currentRecorder = recorder
if (currentRecorder !== null) {
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
return {
code: 0,
data: {},
message: '成功'
}
}
}
return {
code: -1,
data: {},
message: '初始化录音失败'
if (recorder?.getState() == AudioRecord.STATE_INITIALIZED) {
return { code: 0, data: {}, message: '成功' }
} else {
return { code: -1, data: {}, message: '初始化录音失败' }
}
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
return { code: -1, data: {}, message: error.toString() }
}
}
// Final version based on the naming convention from the documentation.
// This function intentionally returns void. The caller on the JS side
// must NOT use `await` on it, otherwise a ClassCastException will occur.
export function onStartRecord(callback: (data: Array<number>) => void) {
stopRecord()
.then(() => {
const currentRecorder = recorder
if (currentRecorder == null) {
console.log('Error: Recorder not initialized.')
return
}
@ -244,7 +209,6 @@ export function onStartRecord(callback: (data: Array<number>) => void) {
isRecording = false
}
})
recordThread!!.start()
})
.catch(error => {
@ -266,25 +230,13 @@ export const stopRecord = async function (): Promise<Result> {
recordThread = null
}
return {
code: 0,
data: {},
message: '成功'
}
}
return {
code: 0,
data: {},
message: '录音未开始'
return { code: 0, data: {}, message: '成功' }
}
return { code: 0, data: {}, message: '录音未开始' }
} catch (error) {
isRecording = false
recordThread = null
return {
code: -1,
data: {},
message: error.toString()
}
return { code: -1, data: {}, message: error.toString() }
}
}
@ -293,7 +245,7 @@ export const releaseRecord = async function (): Promise<Result> {
await stopRecord()
const currentRecorder = recorder
if (currentRecorder !== null) {
if (currentRecorder != null) {
try {
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
currentRecorder.release()
@ -302,16 +254,9 @@ export const releaseRecord = async function (): Promise<Result> {
recorder = null
}
return {
code: 0,
data: {},
message: '成功'
}
return { code: 0, data: {}, message: '成功' }
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
return { code: -1, data: {}, message: error.toString() }
}
}
// @ts-ignore-end