• 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 */
15
16import media from '@ohos.multimedia.media'
17import Logger from '../model/Logger'
18
19const TAG: string = '[Recorder.RecordModel]'
20
21let audioConfig = {
22  audioEncodeBitRate : 22050,
23  audioSampleRate : 22050,
24  numberOfChannels : 2,
25  uri :'',
26  location : { latitude : 30, longitude : 130},
27  audioEncoderMime : media.CodecMimeType.AUDIO_AAC,
28  fileFormat : media.ContainerFormatType.CFT_MPEG_4A,
29}
30
31export class RecordModel {
32  private audioRecorder = undefined
33
34  initAudioRecorder() {
35    this.release();
36    this.audioRecorder = media.createAudioRecorder()
37    Logger.info(TAG, 'create audioRecorder success')
38  }
39
40  release() {
41    if (typeof (this.audioRecorder) != `undefined`) {
42      Logger.info(TAG, 'case audioRecorder  release')
43      this.audioRecorder.release()
44      this.audioRecorder = undefined
45    }
46  }
47
48  startRecorder(pathName, callback) {
49    Logger.info(TAG, `enter the startRecorder,pathName=${pathName}, audioRecorder=${JSON.stringify(this.audioRecorder)}`)
50    if (typeof (this.audioRecorder) != 'undefined') {
51      Logger.info(TAG, 'enter the if')
52      this.audioRecorder.on('prepare', () => {
53        Logger.info(TAG, 'setCallback  prepare case callback is called')
54        this.audioRecorder.start()
55      })
56      this.audioRecorder.on('start', () => {
57        Logger.info(TAG, 'setCallback start case callback is called')
58        callback()
59      })
60      Logger.info(TAG, 'start prepare')
61      audioConfig.uri = pathName
62      this.audioRecorder.prepare(audioConfig)
63    } else {
64      Logger.info(TAG, 'case failed, audioRecorder is null')
65    }
66  }
67
68  pause(callback) {
69    Logger.info(TAG, 'audioRecorder pause called')
70    if (typeof (this.audioRecorder) != `undefined`) {
71      this.audioRecorder.on('pause', () => {
72        Logger.info(TAG, 'audioRecorder pause finish')
73        callback()
74      })
75      this.audioRecorder.pause()
76    }
77  }
78
79  resume(callback) {
80    Logger.info(TAG, 'audioRecorder resume called')
81    if (typeof (this.audioRecorder) != `undefined`) {
82      this.audioRecorder.on('resume', () => {
83        Logger.info(TAG, 'audioRecorder resume finish')
84        callback()
85      })
86      this.audioRecorder.resume()
87    }
88  }
89
90  finish(callback) {
91    if (typeof (this.audioRecorder) != `undefined`) {
92      this.audioRecorder.on('stop', () => {
93        Logger.info(TAG, 'audioRecorder stop called')
94        this.audioRecorder.release()
95        callback()
96      })
97      this.audioRecorder.stop()
98    }
99  }
100}