129 lines
3.3 KiB
JavaScript
129 lines
3.3 KiB
JavaScript
|
|
/**
|
|||
|
|
* Plus Audio录音工具(兼容老设备)
|
|||
|
|
* 使用5+ Runtime原生API,兼容性更好
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
class PlusAudioRecorder {
|
|||
|
|
constructor() {
|
|||
|
|
this.recorder = null
|
|||
|
|
this.isRecording = false
|
|||
|
|
this.recordFile = null
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 开始录音
|
|||
|
|
*/
|
|||
|
|
start() {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
try {
|
|||
|
|
console.log('[Plus录音] 开始录音...')
|
|||
|
|
|
|||
|
|
// 使用plus.audio.getRecorder()
|
|||
|
|
this.recorder = plus.audio.getRecorder()
|
|||
|
|
|
|||
|
|
// 使用AMR格式(最兼容老设备)
|
|||
|
|
const format = 'amr' // amr是最古老最稳定的格式
|
|||
|
|
this.recordFile = '_doc/recordings/' + Date.now() + '.' + format
|
|||
|
|
|
|||
|
|
// 录音参数(AMR格式,电话音质,100%兼容)
|
|||
|
|
const options = {
|
|||
|
|
filename: this.recordFile,
|
|||
|
|
format: format,
|
|||
|
|
samplerate: '8000', // 8000Hz采样率
|
|||
|
|
channels: '1' // 单声道
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('[Plus录音] 配置:', options)
|
|||
|
|
|
|||
|
|
// 开始录音
|
|||
|
|
this.recorder.record(options, () => {
|
|||
|
|
console.log('[Plus录音] 录音已开始')
|
|||
|
|
this.isRecording = true
|
|||
|
|
resolve()
|
|||
|
|
}, (error) => {
|
|||
|
|
console.error('[Plus录音] 启动失败:', error)
|
|||
|
|
reject(error)
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('[Plus录音] 异常:', e)
|
|||
|
|
reject(e)
|
|||
|
|
}
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// #ifndef APP-PLUS
|
|||
|
|
reject(new Error('只支持APP环境'))
|
|||
|
|
// #endif
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 停止录音
|
|||
|
|
*/
|
|||
|
|
stop() {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
try {
|
|||
|
|
if (!this.isRecording) {
|
|||
|
|
reject(new Error('未在录音'))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('[Plus录音] 停止录音...')
|
|||
|
|
|
|||
|
|
this.recorder.stop()
|
|||
|
|
this.isRecording = false
|
|||
|
|
|
|||
|
|
// 等待文件保存
|
|||
|
|
setTimeout(() => {
|
|||
|
|
// 验证文件
|
|||
|
|
plus.io.resolveLocalFileSystemURL(this.recordFile, (entry) => {
|
|||
|
|
entry.file((file) => {
|
|||
|
|
console.log('[Plus录音] 文件信息:', {
|
|||
|
|
name: file.name,
|
|||
|
|
size: file.size,
|
|||
|
|
type: file.type
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
if (file.size < 100) {
|
|||
|
|
console.error('[Plus录音] 文件过小:', file.size, 'bytes')
|
|||
|
|
reject(new Error('录音文件过小,可能录音失败'))
|
|||
|
|
} else {
|
|||
|
|
console.log('[Plus录音] 录音成功,文件:', this.recordFile, '大小:', file.size)
|
|||
|
|
resolve(this.recordFile)
|
|||
|
|
}
|
|||
|
|
}, reject)
|
|||
|
|
}, (error) => {
|
|||
|
|
console.error('[Plus录音] 文件不存在:', error)
|
|||
|
|
reject(error)
|
|||
|
|
})
|
|||
|
|
}, 1000)
|
|||
|
|
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('[Plus录音] 停止异常:', e)
|
|||
|
|
reject(e)
|
|||
|
|
}
|
|||
|
|
// #endif
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 播放录音
|
|||
|
|
*/
|
|||
|
|
play(filePath) {
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
try {
|
|||
|
|
const player = plus.audio.createPlayer(filePath)
|
|||
|
|
player.play()
|
|||
|
|
return player
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('[Plus录音] 播放失败:', e)
|
|||
|
|
return null
|
|||
|
|
}
|
|||
|
|
// #endif
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default new PlusAudioRecorder()
|