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 'java.lang.Thread'
|
||||||
import { UTSAndroid } from 'io.dcloud.uts'
|
import { UTSAndroid } from 'io.dcloud.uts'
|
||||||
import { Result } from '../interface.uts'
|
import { Result } from '../interface.uts'
|
||||||
// @ts-ignore-end
|
|
||||||
import { FLVPacker, FLVListener } from 'com.tencent.iot.thirdparty.flv'
|
import { FLVPacker, FLVListener } from 'com.tencent.iot.thirdparty.flv'
|
||||||
|
|
||||||
let recorder: AudioRecord | null = null
|
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> {
|
// Final version based on the naming convention from the documentation.
|
||||||
try {
|
// This function intentionally returns void. The caller on the JS side
|
||||||
await stopRecord()
|
// must NOT use `await` on it, otherwise a ClassCastException will occur.
|
||||||
|
export function onStartRecord(callback: (data: Array<number>) => void) {
|
||||||
|
stopRecord()
|
||||||
|
.then(() => {
|
||||||
const currentRecorder = recorder
|
const currentRecorder = recorder
|
||||||
if (currentRecorder == null) {
|
if (currentRecorder == null) {
|
||||||
return { code: -1, data: {}, message: '录音尚未初始化' }
|
console.log('Error: Recorder not initialized.')
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const listener = new MyFLVListener(callback)
|
const listener = new MyFLVListener(callback)
|
||||||
@ -183,7 +185,6 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
|||||||
|
|
||||||
const audioInfo = new MediaCodec.BufferInfo()
|
const audioInfo = new MediaCodec.BufferInfo()
|
||||||
while (isRecording) {
|
while (isRecording) {
|
||||||
// Feed encoder
|
|
||||||
const inputBufferId = aacEncoder!!.dequeueInputBuffer(10000)
|
const inputBufferId = aacEncoder!!.dequeueInputBuffer(10000)
|
||||||
if (inputBufferId >= 0) {
|
if (inputBufferId >= 0) {
|
||||||
const inputBuffer = aacEncoder!!.getInputBuffer(inputBufferId)!!
|
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)
|
var outputBufferId = aacEncoder!!.dequeueOutputBuffer(audioInfo, 10000)
|
||||||
while (outputBufferId >= 0 && isRecording) {
|
while (outputBufferId >= 0 && isRecording) {
|
||||||
const outputBuffer = aacEncoder!!.getOutputBuffer(outputBufferId)!!
|
const outputBuffer = aacEncoder!!.getOutputBuffer(outputBufferId)!!
|
||||||
if (audioInfo.size > 0 && flvPacker != null) {
|
if (audioInfo.size > 0 && flvPacker != null) {
|
||||||
const outDataSize = audioInfo.size
|
const outDataSize = audioInfo.size
|
||||||
const packetLen = outDataSize + 7
|
const aacPacketWithAdts = ByteArray(outDataSize + 7)
|
||||||
const aacPacket = ByteArray(packetLen)
|
|
||||||
|
|
||||||
addADTStoPacket(aacPacket, packetLen)
|
addADTStoPacket(aacPacketWithAdts, outDataSize + 7)
|
||||||
|
|
||||||
outputBuffer.position(audioInfo.offset)
|
outputBuffer.position(audioInfo.offset)
|
||||||
outputBuffer.limit(audioInfo.offset + outDataSize)
|
outputBuffer.limit(audioInfo.offset + outDataSize)
|
||||||
outputBuffer.get(aacPacket, 7, outDataSize)
|
outputBuffer.get(aacPacketWithAdts, 7, outDataSize)
|
||||||
outputBuffer.position(audioInfo.offset)
|
|
||||||
|
|
||||||
if (flvPacker != null && isRecording) {
|
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)
|
aacEncoder!!.releaseOutputBuffer(outputBufferId, false)
|
||||||
@ -224,7 +226,7 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log error
|
console.log('Record thread error: ' + error.toString())
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
currentRecorder?.stop()
|
currentRecorder?.stop()
|
||||||
@ -244,19 +246,10 @@ export async function onStartRecord(callback: (data: Array<number>) => void): Pr
|
|||||||
})
|
})
|
||||||
|
|
||||||
recordThread!!.start()
|
recordThread!!.start()
|
||||||
|
})
|
||||||
return {
|
.catch(error => {
|
||||||
code: 0,
|
console.log('Error in stopRecord(): ' + error.toString())
|
||||||
data: {},
|
})
|
||||||
message: '成功'
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: error.toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const stopRecord = async function (): Promise<Result> {
|
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