• 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 fileIO from '@ohos.fileio'
18
19import { Action } from '../../redux/actions/Action'
20import { Log } from '../../utils/Log'
21import { EventBus } from '../../worker/eventbus/EventBus'
22import EventBusManager from '../../worker/eventbus/EventBusManager'
23import { SettingManager } from '../../setting/SettingManager'
24import { Voice } from '../../setting/settingitem/Voice'
25
26export class PlaySound {
27  private TAG: string = '[PlaySound]:'
28  private appEventBus: EventBus = EventBusManager.getInstance().getEventBus()
29  private settingManager = SettingManager.getInstance()
30  private fdNumber: number = 0
31  private fdPath
32  private mAudioPlayer:media.AudioPlayer
33
34  constructor() {
35   this.appEventBus.on(Action.ACTION_CAPTURE, this.playCapture.bind(this))
36    this.mAudioPlayer = media.createAudioPlayer()
37    this.setAudioCallback()
38  }
39
40  setAudioCallback() {
41    this.mAudioPlayer.on('error', (err) => {
42      Log.info(`${this.TAG} case error called, err is: ${err.message}`)
43      Log.info(`${this.TAG} case error called, status is: ${this.mAudioPlayer.state}`)
44    })
45    this.mAudioPlayer.on('dataLoad', () => {
46      Log.info(`${this.TAG} case dataLoad called, status is: ${this.mAudioPlayer.state}`)
47      if (this.settingManager.getCaptureMute() == Voice.SOUND) {
48        this.mAudioPlayer.play()
49      }
50    })
51    this.mAudioPlayer.on('play', () => {
52      Log.info(`${this.TAG} case play called, status is: ${this.mAudioPlayer.state}`)
53    })
54    this.mAudioPlayer.on('reset', () => {
55      Log.info(`${this.TAG} case reset called, status is: ${this.mAudioPlayer.state}`)
56      this.mAudioPlayer.src = this.fdPath
57    })
58    this.mAudioPlayer.on('finish', () => {
59      Log.info(`${this.TAG} case finish called, status is: ${this.mAudioPlayer.state}`)
60        if (this.fdNumber !== 0) {
61          fileIO.closeSync(this.fdNumber)
62          Log.info(`${this.TAG} fileIO closeSync success`)
63        }
64    })
65  }
66
67  public static getInstance(): PlaySound {
68    if (!globalThis?.sInstancePlaySound) {
69      globalThis.sInstancePlaySound = new PlaySound()
70    }
71    return globalThis.sInstancePlaySound;
72  }
73
74  private playRecordStart(data) {
75    Log.debug(`${this.TAG} playRecordStart invoke E`)
76    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
77      this.playSound(Voice.RECORD_START_URI)
78    }
79    Log.debug(`${this.TAG} playRecordStart invoke X`)
80  }
81
82  private playRecordStop(data) {
83    Log.debug(`${this.TAG} playRecordStop invoke E`)
84    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
85      this.playSound(Voice.RECORD_STOP_URI)
86    }
87    Log.debug(`${this.TAG} playRecordStop invoke X`)
88  }
89
90  private playCapture(data) {
91    Log.debug(`${this.TAG} playCapture invoke E`)
92    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
93      this.playSound(Voice.CAPTURE_URI)
94    }
95    Log.debug(`${this.TAG} playCapture invoke X`)
96  }
97
98  private async playSound(soundUri) {
99    Log.info(`${this.TAG} playSound invoke E`)
100    this.fdPath = 'fd://'
101    fileIO.open(Voice.CAPTURE_URI).then((fdData) => {
102      this.fdPath = this.fdPath + '' + fdData
103      this.fdNumber = fdData
104      this.mAudioPlayer.src = this.fdPath
105      this.mAudioPlayer.reset()
106    }).catch((err) => {
107      Log.info(`${this.TAG} open soundUri failed, err: ${err}`)
108    })
109    Log.info(`${this.TAG} playSound invoke X`)
110  }
111}