refactor: 优化音频录制相关函数,简化代码结构,增强错误处理逻辑
This commit is contained in:
parent
6d538569f7
commit
ffe0cada1c
@ -42,9 +42,9 @@ class MyFLVListener extends FLVListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addADTStoPacket(packet: ByteArray, packetLen: Int) {
|
function addADTStoPacket(packet: ByteArray, packetLen: Int) {
|
||||||
const profile = 2 // AAC LC
|
const profile = 2
|
||||||
const freqIdx = 8 // 16000Hz
|
const freqIdx = 8
|
||||||
const chanCfg = 1 // MONO
|
const chanCfg = 1
|
||||||
|
|
||||||
packet[0] = (0xff).toByte()
|
packet[0] = (0xff).toByte()
|
||||||
packet[1] = (0xf9).toByte()
|
packet[1] = (0xf9).toByte()
|
||||||
@ -58,40 +58,23 @@ function addADTStoPacket(packet: ByteArray, packetLen: Int) {
|
|||||||
export const requestPermission = async function (): Promise<Result> {
|
export const requestPermission = async function (): Promise<Result> {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
try {
|
try {
|
||||||
let permissionNeed = ['android.permission.RECORD_AUDIO']
|
const permissionNeed = ['android.permission.RECORD_AUDIO']
|
||||||
|
|
||||||
UTSAndroid.requestSystemPermission(
|
UTSAndroid.requestSystemPermission(
|
||||||
UTSAndroid.getUniActivity()!,
|
UTSAndroid.getUniActivity()!,
|
||||||
permissionNeed,
|
permissionNeed,
|
||||||
function (allRight: boolean, _: string[]) {
|
(allRight: boolean, _: string[]) => {
|
||||||
if (allRight) {
|
if (allRight) {
|
||||||
resolve({
|
resolve({ code: 0, data: {}, message: '成功' })
|
||||||
code: 0,
|
|
||||||
data: {},
|
|
||||||
message: '成功'
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
resolve({
|
resolve({ code: -1, data: {}, message: '失败' })
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: '失败'
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
function (_: boolean, _: string[]) {
|
(_: boolean, _: string[]) => {
|
||||||
resolve({
|
resolve({ code: -1, data: {}, message: '失败' })
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: '失败'
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
resolve({
|
resolve({ code: -1, data: {}, message: error.toString() })
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: error.toString()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -117,39 +100,21 @@ export const initAudio = async function (): Promise<Result> {
|
|||||||
bufferSizeInBytes
|
bufferSizeInBytes
|
||||||
)
|
)
|
||||||
|
|
||||||
const currentRecorder = recorder
|
if (recorder?.getState() == AudioRecord.STATE_INITIALIZED) {
|
||||||
if (currentRecorder !== null) {
|
return { code: 0, data: {}, message: '成功' }
|
||||||
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
|
} else {
|
||||||
return {
|
return { code: -1, data: {}, message: '初始化录音失败' }
|
||||||
code: 0,
|
|
||||||
data: {},
|
|
||||||
message: '成功'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: '初始化录音失败'
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return { code: -1, data: {}, message: error.toString() }
|
||||||
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) {
|
export function onStartRecord(callback: (data: Array<number>) => void) {
|
||||||
stopRecord()
|
stopRecord()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const currentRecorder = recorder
|
const currentRecorder = recorder
|
||||||
if (currentRecorder == null) {
|
if (currentRecorder == null) {
|
||||||
console.log('Error: Recorder not initialized.')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,7 +209,6 @@ export function onStartRecord(callback: (data: Array<number>) => void) {
|
|||||||
isRecording = false
|
isRecording = false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
recordThread!!.start()
|
recordThread!!.start()
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
@ -266,25 +230,13 @@ export const stopRecord = async function (): Promise<Result> {
|
|||||||
recordThread = null
|
recordThread = null
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return { code: 0, data: {}, message: '成功' }
|
||||||
code: 0,
|
|
||||||
data: {},
|
|
||||||
message: '成功'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
code: 0,
|
|
||||||
data: {},
|
|
||||||
message: '录音未开始'
|
|
||||||
}
|
}
|
||||||
|
return { code: 0, data: {}, message: '录音未开始' }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
isRecording = false
|
isRecording = false
|
||||||
recordThread = null
|
recordThread = null
|
||||||
return {
|
return { code: -1, data: {}, message: error.toString() }
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: error.toString()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +245,7 @@ export const releaseRecord = async function (): Promise<Result> {
|
|||||||
await stopRecord()
|
await stopRecord()
|
||||||
|
|
||||||
const currentRecorder = recorder
|
const currentRecorder = recorder
|
||||||
if (currentRecorder !== null) {
|
if (currentRecorder != null) {
|
||||||
try {
|
try {
|
||||||
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
|
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
|
||||||
currentRecorder.release()
|
currentRecorder.release()
|
||||||
@ -302,16 +254,9 @@ export const releaseRecord = async function (): Promise<Result> {
|
|||||||
recorder = null
|
recorder = null
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return { code: 0, data: {}, message: '成功' }
|
||||||
code: 0,
|
|
||||||
data: {},
|
|
||||||
message: '成功'
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return { code: -1, data: {}, message: error.toString() }
|
||||||
code: -1,
|
|
||||||
data: {},
|
|
||||||
message: error.toString()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// @ts-ignore-end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user