210 lines
4.7 KiB
Plaintext
210 lines
4.7 KiB
Plaintext
/* eslint-disable */
|
|
// @ts-nocheck
|
|
// @ts-ignore-start
|
|
import 'android.media.AudioRecord'
|
|
import 'android.media.MediaRecorder'
|
|
import 'android.media.AudioFormat'
|
|
import 'android.media.MediaSyncEvent'
|
|
import 'java.lang.Thread'
|
|
import { UTSAndroid } from 'io.dcloud.uts'
|
|
import { Result } from '../interface.uts'
|
|
|
|
let recorder: AudioRecord | null = null
|
|
let recorderState: boolean = false
|
|
let bufferSizeInBytes: Int = 0
|
|
|
|
export const requestPermission = async function (): Promise<Result> {
|
|
return new Promise(resolve => {
|
|
try {
|
|
let permissionNeed = ['android.permission.RECORD_AUDIO']
|
|
|
|
UTSAndroid.requestSystemPermission(
|
|
UTSAndroid.getUniActivity()!,
|
|
permissionNeed,
|
|
function (allRight: boolean, _: string[]) {
|
|
if (allRight) {
|
|
resolve({
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
})
|
|
} else {
|
|
resolve({
|
|
code: -1,
|
|
data: {},
|
|
message: '失败'
|
|
})
|
|
}
|
|
},
|
|
function (_: boolean, _: string[]) {
|
|
resolve({
|
|
code: -1,
|
|
data: {},
|
|
message: '失败'
|
|
})
|
|
}
|
|
)
|
|
} catch (error) {
|
|
resolve({
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
export const initAudio = async function (): Promise<Result> {
|
|
try {
|
|
const audioSource = MediaRecorder.AudioSource.MIC
|
|
const sampleRateInHz = 44100
|
|
const channelConfig = AudioFormat.CHANNEL_IN_MONO
|
|
const audioFormat = AudioFormat.ENCODING_PCM_16BIT
|
|
|
|
bufferSizeInBytes = AudioRecord.getMinBufferSize(
|
|
sampleRateInHz.toInt(),
|
|
channelConfig,
|
|
audioFormat
|
|
)
|
|
|
|
console.log('bufferSizeInBytes', bufferSizeInBytes)
|
|
|
|
recorder = new AudioRecord(
|
|
audioSource,
|
|
sampleRateInHz.toInt(),
|
|
channelConfig,
|
|
audioFormat,
|
|
bufferSizeInBytes
|
|
)
|
|
|
|
const currentRecorder = recorder
|
|
if (currentRecorder !== null) {
|
|
console.log('初始化录音结果:', currentRecorder.getState())
|
|
if (currentRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
|
|
return {
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: '初始化录音失败'
|
|
}
|
|
} catch (error) {
|
|
console.log('初始化录音失败', error)
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function onStartRecord(callback: (data: Array) => void): Promise<Result> {
|
|
try {
|
|
const callbackFunction = () => {
|
|
const currentRecorder = recorder
|
|
if (currentRecorder !== null) {
|
|
while (recorderState) {
|
|
let audioData = new ByteArray(bufferSizeInBytes)
|
|
const result: Int = currentRecorder.read(audioData, 0, bufferSizeInBytes)
|
|
if (result > 0) {
|
|
callback(Array.fromNative(audioData))
|
|
console.log('录音按帧返回数据', Array.fromNative(audioData))
|
|
}
|
|
Thread.sleep(10)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
const currentRecorder = recorder
|
|
if (currentRecorder !== null) {
|
|
currentRecorder.startRecording()
|
|
console.log('开始录音')
|
|
recorderState = true
|
|
|
|
callbackFunction()
|
|
|
|
return {
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
}
|
|
}
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: '开始录音失败'
|
|
}
|
|
} catch (error) {
|
|
console.log('开始录音失败', error)
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
export const stopRecord = async function (): Promise<Result> {
|
|
try {
|
|
const currentRecorder = recorder
|
|
if (currentRecorder !== null) {
|
|
currentRecorder.stop()
|
|
recorderState = false
|
|
console.log('停止录音')
|
|
return {
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
}
|
|
}
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: '停止录音失败'
|
|
}
|
|
} catch (error) {
|
|
console.log('停止录音失败', error)
|
|
recorderState = false
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
export const releaseRecord = async function (): Promise<Result> {
|
|
try {
|
|
const currentRecorder = recorder
|
|
if (currentRecorder !== null) {
|
|
currentRecorder.release()
|
|
console.log('释放录音')
|
|
return {
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
}
|
|
}
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: '释放录音失败'
|
|
}
|
|
} catch (error) {
|
|
console.log('释放录音失败', error)
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
// @ts-ignore-end
|