feat: 优化音频录制功能,调整onStartRecord函数以支持Promise,增强错误处理和日志记录
This commit is contained in:
parent
601bfb1e16
commit
6d538569f7
@ -11,7 +11,6 @@ import 'android.media.MediaFormat'
|
||||
import 'java.lang.Thread'
|
||||
import { UTSAndroid } from 'io.dcloud.uts'
|
||||
import { Result } from '../interface.uts'
|
||||
// @ts-ignore-end
|
||||
import { FLVPacker, FLVListener } from 'com.tencent.iot.thirdparty.flv'
|
||||
|
||||
let recorder: AudioRecord | null = null
|
||||
@ -142,13 +141,16 @@ export const initAudio = async function (): Promise<Result> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function onStartRecord(callback: (data: Array<number>) => void): Promise<Result> {
|
||||
try {
|
||||
await stopRecord()
|
||||
|
||||
// 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) {
|
||||
return { code: -1, data: {}, message: '录音尚未初始化' }
|
||||
console.log('Error: Recorder not initialized.')
|
||||
return
|
||||
}
|
||||
|
||||
const listener = new MyFLVListener(callback)
|
||||
@ -183,7 +185,6 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
||||
|
||||
const audioInfo = new MediaCodec.BufferInfo()
|
||||
while (isRecording) {
|
||||
// Feed encoder
|
||||
const inputBufferId = aacEncoder!!.dequeueInputBuffer(10000)
|
||||
if (inputBufferId >= 0) {
|
||||
const inputBuffer = aacEncoder!!.getInputBuffer(inputBufferId)!!
|
||||
@ -199,24 +200,25 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
||||
}
|
||||
}
|
||||
|
||||
// Drain encoder
|
||||
var outputBufferId = aacEncoder!!.dequeueOutputBuffer(audioInfo, 10000)
|
||||
while (outputBufferId >= 0 && isRecording) {
|
||||
const outputBuffer = aacEncoder!!.getOutputBuffer(outputBufferId)!!
|
||||
if (audioInfo.size > 0 && flvPacker != null) {
|
||||
const outDataSize = audioInfo.size
|
||||
const packetLen = outDataSize + 7
|
||||
const aacPacket = ByteArray(packetLen)
|
||||
const aacPacketWithAdts = ByteArray(outDataSize + 7)
|
||||
|
||||
addADTStoPacket(aacPacket, packetLen)
|
||||
addADTStoPacket(aacPacketWithAdts, outDataSize + 7)
|
||||
|
||||
outputBuffer.position(audioInfo.offset)
|
||||
outputBuffer.limit(audioInfo.offset + outDataSize)
|
||||
outputBuffer.get(aacPacket, 7, outDataSize)
|
||||
outputBuffer.position(audioInfo.offset)
|
||||
outputBuffer.get(aacPacketWithAdts, 7, outDataSize)
|
||||
|
||||
if (flvPacker != null && isRecording) {
|
||||
flvPacker!!.encodeFlv(aacPacket, FLVPacker.TYPE_AUDIO, Date.now().toLong())
|
||||
flvPacker!!.encodeFlv(
|
||||
aacPacketWithAdts,
|
||||
FLVPacker.TYPE_AUDIO,
|
||||
Date.now().toLong()
|
||||
)
|
||||
}
|
||||
}
|
||||
aacEncoder!!.releaseOutputBuffer(outputBufferId, false)
|
||||
@ -224,7 +226,7 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Log error
|
||||
console.log('Record thread error: ' + error.toString())
|
||||
} finally {
|
||||
try {
|
||||
currentRecorder?.stop()
|
||||
@ -244,19 +246,10 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
||||
})
|
||||
|
||||
recordThread!!.start()
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
data: {},
|
||||
message: '成功'
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
code: -1,
|
||||
data: {},
|
||||
message: error.toString()
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('Error in stopRecord(): ' + error.toString())
|
||||
})
|
||||
}
|
||||
|
||||
export const stopRecord = async function (): Promise<Result> {
|
||||
@ -322,5 +315,3 @@ export const releaseRecord = async function (): Promise<Result> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore-end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user