Openharmony音频播放器

Crips
发布于 2023-12-24 21:12
浏览
0收藏

音频录制流程

1.使用createdAudioRecorder接口获取AudioRecorder实例

initAudioRecorder(){
this.release();
this.audioRecorder=media.createAudioRecorder();
Logger.info(TAG,‘create audioRecorder success’)
}

2.准备设备的录制参数

let audioConfig={
audioEncodeBitRate:22050,
audioSampleRate:22050,
numberOfChangeIs:2,
uri:‘’,
location:{ latitude:30,longitude:130},audioEncoderMime:media.CodecMimeType.AUDIO_AAC,
fileFormat:media.ContainerFormatType.CFT_MPEG_4A,
}

3.AudioRecorder实例调用prepare函数来录制

startRecorder(pathName,callback){
Logger.info(TAG,‘enter the startRecorder,pathName=${pathName},audioRecorder=${JSON.stringify(this.audioRecorder)}’)
if(typeof(this.audioRecorder)!=‘undefined’){
Logger.info(TAG,‘enter the if’)
this.audioRecorder.on(‘prepare’,()=>{
Logger.info(TAG,‘setCallback prepare case callback is called’)
this.audioRecorder.start()
})
this.audioRecorder.on(‘start’,()=>{
Logger.info(TAG,‘setCall start case callback is called’)
callback()
})
Logger.info(TAG,‘start prepare’)
audioConfig.uri=pathName
this.audioRecorder.prepare(audioConfig)
}
else{
Logger.info(TAG,‘case failed,audioRecorder is null’)
}
}

4.录制过程中通过pause和resume来完成录音的暂停和重新开始

pause(callback){
Logger.info(TAG,‘audioRecorder pause called’)
if(typeof(this.audioRecorder)!=‘undefined’){
this.audioRecorder.on(‘pause’,()=>{
Logger.info(TAG,‘audioRecorder pause finish’)
callback()
})
this.audioRecorder.pause()
}
}
resume(callback){
Logger.info(TAG,‘audioRecorder resume called’)
if(typeof(this.audioRecorder)!=‘undefined’){
this.audioRecorder.on(‘resume’,()=>{
Logger.info(TAG,‘audioRecorder resume finish’)
callback()
})
this.audioRecorder.resume()
}
}

5.录制结束后释放资源

finish(callback){
if(typeof(this.audioRecorder)!=‘undefined’){
this.audioRecorder.on(‘stop’,()=>{
Logger.info(TAG,‘audioRecorder stop called’)
this.audioRecorder.release()
callback()
})
this.audioRecorder.stop()
}
}

收藏
回复
举报
回复
    相关推荐