• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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    // @ts-ignore
33  private mAVPlayer: media.AVPlayer
34
35  constructor() {
36    Log.info(`${this.TAG} constructor start, enroll playCapture to ACTION_CAPTURE`)
37    this.appEventBus.on(Action.ACTION_CAPTURE, this.playCapture.bind(this))
38      // @ts-ignore
39    media.createAVPlayer().then((avPlayer) => {
40      Log.info(`${this.TAG} createAVPlayer then ${avPlayer}`)
41      this.mAVPlayer = avPlayer;
42      this.setAVPlayerCallback();
43    })
44  }
45
46  setAVPlayerCallback() {
47    Log.info(`${this.TAG} setAudioCallback`)
48    this.mAVPlayer.on('stateChange', async (state, reason) => {
49      switch (state) {
50        case 'idle':
51          Log.info(`${this.TAG} stateChange idle state`)
52          break;
53        case 'initialized':
54          Log.info(`${this.TAG} stateChange initialized state`)
55          this.mAVPlayer.prepare().then(() => {}, (err) => {
56            Log.error(`${this.TAG} case prepare error`)
57          })
58          break;
59        case 'prepared':
60          Log.info(`${this.TAG} stateChange prepared state`)
61          if (this.settingManager.getCaptureMute() == Voice.SOUND) {
62            this.mAVPlayer.play()
63          }
64          break;
65        case 'playing':
66          Log.info(`${this.TAG} stateChange playing state`)
67          break;
68        case 'completed':
69          Log.info(`${this.TAG} stateChange completed state`)
70          if (this.fdNumber !== 0) {
71            fileIO.closeSync(this.fdNumber)
72            Log.info(`${this.TAG} fileIO closeSync success`)
73          }
74          this.mAVPlayer.stop()
75          break;
76        case 'stopped':
77          Log.info(`${this.TAG} stateChange stopped state`)
78          this.mAVPlayer.reset()
79          break;
80        case 'error':
81          Log.info(`${this.TAG} case error called, err is: ${reason}`)
82          break;
83        default:
84          Log.info(`${this.TAG} unknow state: ${state}`)
85          break;
86      }
87    })
88  }
89
90  public static getInstance(): PlaySound {
91    if (!globalThis?.sInstancePlaySound) {
92      globalThis.sInstancePlaySound = new PlaySound()
93    }
94    return globalThis.sInstancePlaySound;
95  }
96
97  private playRecordStart(data) {
98    Log.debug(`${this.TAG} playRecordStart invoke E`)
99    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
100      this.playSound(Voice.RECORD_START_URI)
101    }
102    Log.debug(`${this.TAG} playRecordStart invoke X`)
103  }
104
105  private playRecordStop(data) {
106    Log.debug(`${this.TAG} playRecordStop invoke E`)
107    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
108      this.playSound(Voice.RECORD_STOP_URI)
109    }
110    Log.debug(`${this.TAG} playRecordStop invoke X`)
111  }
112
113  private playCapture(data) {
114    Log.debug(`${this.TAG} playCapture invoke E`)
115    if (this.settingManager.getCaptureMute() == Voice.SOUND) {
116      this.playSound(Voice.CAPTURE_URI)
117    }
118    Log.debug(`${this.TAG} playCapture invoke X`)
119  }
120
121  private async playSound(soundUri) {
122    Log.info(`${this.TAG} playSound invoke E`)
123    this.fdPath = 'fd://'
124    fileIO.open(Voice.CAPTURE_URI).then((fdData) => {
125      this.fdPath = this.fdPath + '' + fdData
126      this.mAVPlayer.url = this.fdPath
127      this.fdNumber = fdData
128      Log.debug(`${this.TAG} fileIO open then ${this.fdPath}`)
129    }).catch((err) => {
130      Log.info(`${this.TAG} open soundUri failed, err: ${err}`)
131    })
132    Log.info(`${this.TAG} playSound invoke X`)
133  }
134}