1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import media from '@ohos.multimedia.media' 17import Logger from '../model/Logger' 18 19let audioConfig = { 20 audioSourceType: 1, 21 audioEncoder: 3, 22 audioEncodeBitRate: 22050, 23 audioSampleRate: 22050, 24 numberOfChannels: 2, 25 format: 6, 26 uri: '' 27} 28 29export default class RecordModel { 30 private tag: string = 'qlw RecordModel' 31 private audioRecorder: media.AudioRecorder = undefined 32 33 initAudioRecorder(handleStateChange: () => void) { 34 this.release(); 35 this.audioRecorder = media.createAudioRecorder() 36 Logger.info(this.tag, 'create audioRecorder success') 37 this.audioRecorder.on('prepare', () => { 38 Logger.info(this.tag, 'setCallback prepare case callback is called') 39 this.audioRecorder.start() 40 }) 41 this.audioRecorder.on('start', () => { 42 Logger.info(this.tag, 'setCallback start case callback is called') 43 handleStateChange() 44 }) 45 this.audioRecorder.on('stop', () => { 46 Logger.info(this.tag, 'audioRecorder stop called') 47 this.audioRecorder.release() 48 }) 49 this.audioRecorder.on('pause', () => { 50 Logger.info(this.tag, 'audioRecorder pause finish') 51 handleStateChange() 52 }) 53 this.audioRecorder.on('resume', () => { 54 Logger.info(this.tag, 'audioRecorder resume finish') 55 handleStateChange() 56 }) 57 } 58 59 release() { 60 if (typeof (this.audioRecorder) !== `undefined`) { 61 Logger.info(this.tag, 'audioRecorder release') 62 this.audioRecorder.release() 63 this.audioRecorder = undefined 64 } 65 } 66 67 startRecorder(pathName: string) { 68 Logger.info(this.tag, `startRecorder, pathName = ${pathName}`) 69 if (typeof (this.audioRecorder) !== 'undefined') { 70 Logger.info(this.tag, 'start prepare') 71 audioConfig.uri = pathName 72 this.audioRecorder.prepare(audioConfig) 73 } else { 74 Logger.error(this.tag, 'case failed, audioRecorder is null') 75 } 76 } 77 78 pause() { 79 Logger.info(this.tag, 'audioRecorder pause called') 80 if (typeof (this.audioRecorder) !== `undefined`) { 81 this.audioRecorder.pause() 82 } 83 } 84 85 resume() { 86 Logger.info(this.tag, 'audioRecorder resume called') 87 if (typeof (this.audioRecorder) !== `undefined`) { 88 this.audioRecorder.resume() 89 } 90 } 91 92 finish() { 93 if (typeof (this.audioRecorder) !== `undefined`) { 94 this.audioRecorder.stop() 95 } 96 } 97}