• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import media from '@ohos.multimedia.media'
16import Logger from '../model/Logger'
17
18let audioConfig = {
19  audioSourceType: 1,
20  audioEncoder: 3,
21  audioEncodeBitRate: 22050,
22  audioSampleRate: 22050,
23  numberOfChannels: 2,
24  format: 6,
25  uri: ''
26}
27
28export default class RecordModel {
29  private tag: string = 'RecordModel'
30  private audioRecorder: media.AudioRecorder = undefined
31
32  initAudioRecorder(handleStateChange: () => void) {
33    this.release();
34    this.audioRecorder = media.createAudioRecorder()
35    Logger.info(this.tag, 'create audioRecorder success')
36    this.audioRecorder.on('prepare', () => {
37      Logger.info(this.tag, 'setCallback  prepare case callback is called')
38      this.audioRecorder.start()
39    })
40    this.audioRecorder.on('start', () => {
41      Logger.info(this.tag, 'setCallback start case callback is called')
42      handleStateChange()
43    })
44    this.audioRecorder.on('stop', () => {
45      Logger.info(this.tag, 'audioRecorder stop called')
46      this.audioRecorder.release()
47    })
48    this.audioRecorder.on('pause', () => {
49      Logger.info(this.tag, 'audioRecorder pause finish')
50      handleStateChange()
51    })
52    this.audioRecorder.on('resume', () => {
53      Logger.info(this.tag, 'audioRecorder resume finish')
54      handleStateChange()
55    })
56  }
57
58  release() {
59    if (typeof (this.audioRecorder) !== `undefined`) {
60      Logger.info(this.tag, 'audioRecorder  release')
61      this.audioRecorder.release()
62      this.audioRecorder = undefined
63    }
64  }
65
66  startRecorder(pathName: string) {
67    Logger.info(this.tag, `startRecorder, pathName = ${pathName}`)
68    if (typeof (this.audioRecorder) !== 'undefined') {
69      Logger.info(this.tag, 'start prepare')
70      audioConfig.uri = pathName
71      this.audioRecorder.prepare(audioConfig)
72    } else {
73      Logger.error(this.tag, 'case failed, audioRecorder is null')
74    }
75  }
76
77  pause() {
78    Logger.info(this.tag, 'audioRecorder pause called')
79    if (typeof (this.audioRecorder) !== `undefined`) {
80      this.audioRecorder.pause()
81    }
82  }
83
84  resume() {
85    Logger.info(this.tag, 'audioRecorder resume called')
86    if (typeof (this.audioRecorder) !== `undefined`) {
87      this.audioRecorder.resume()
88    }
89  }
90
91  finish() {
92    if (typeof (this.audioRecorder) !== `undefined`) {
93      this.audioRecorder.stop()
94    }
95  }
96}