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 static sInstancePlaySound: PlaySound; 28 private TAG: string = '[PlaySound]:' 29 private appEventBus: EventBus = EventBusManager.getInstance().getEventBus() 30 private settingManager = SettingManager.getInstance() 31 private fdNumber: number = 0 32 private fdPath 33 // @ts-ignore 34 private mAVPlayer: media.AVPlayer 35 36 constructor() { 37 Log.info(`${this.TAG} constructor start, enroll playCapture to ACTION_CAPTURE`) 38 this.appEventBus.on(Action.ACTION_CAPTURE, this.playCapture.bind(this)) 39 // @ts-ignore 40 media.createAVPlayer().then((avPlayer) => { 41 Log.info(`${this.TAG} createAVPlayer then ${avPlayer}`) 42 this.mAVPlayer = avPlayer; 43 this.setAVPlayerCallback(); 44 }) 45 } 46 47 public static getInstance(): PlaySound { 48 if (!PlaySound.sInstancePlaySound) { 49 PlaySound.sInstancePlaySound = new PlaySound(); 50 } 51 return PlaySound.sInstancePlaySound; 52 } 53 54 setAVPlayerCallback(): void { 55 Log.info(`${this.TAG} setAudioCallback`) 56 this.mAVPlayer.on('stateChange', async (state, reason) => { 57 switch (state) { 58 case 'idle': 59 Log.info(`${this.TAG} stateChange idle state`) 60 break; 61 case 'initialized': 62 Log.info(`${this.TAG} stateChange initialized state`) 63 this.mAVPlayer.prepare().then(() => { 64 }, (err) => { 65 Log.error(`${this.TAG} case prepare error`) 66 }) 67 break; 68 case 'prepared': 69 Log.info(`${this.TAG} stateChange prepared state`) 70 if (this.settingManager.getCaptureMute() == Voice.SOUND) { 71 this.mAVPlayer.play() 72 } 73 break; 74 case 'playing': 75 Log.info(`${this.TAG} stateChange playing state`) 76 break; 77 case 'completed': 78 Log.info(`${this.TAG} stateChange completed state`) 79 if (this.fdNumber !== 0) { 80 fileIO.closeSync(this.fdNumber) 81 Log.info(`${this.TAG} fileIO closeSync success`) 82 } 83 this.mAVPlayer.stop() 84 break; 85 case 'stopped': 86 Log.info(`${this.TAG} stateChange stopped state`) 87 this.mAVPlayer.reset() 88 break; 89 case 'error': 90 Log.info(`${this.TAG} case error called, err is: ${reason}`) 91 break; 92 default: 93 Log.info(`${this.TAG} unknow state: ${state}`) 94 break; 95 } 96 }) 97 } 98 99 private playCapture(data): void { 100 Log.info(`${this.TAG} playCapture invoke E`); 101 if (this.settingManager.getCaptureMute() == Voice.SOUND) { 102 this.playSound(Voice.CAPTURE_URI) 103 } 104 Log.info(`${this.TAG} playCapture invoke X`); 105 } 106 107 private async playSound(soundUri) { 108 Log.info(`${this.TAG} playSound invoke E`) 109 this.fdPath = 'fd://' 110 fileIO.open(Voice.CAPTURE_URI).then((fdData) => { 111 this.fdPath = this.fdPath + '' + fdData 112 this.mAVPlayer.url = this.fdPath 113 this.fdNumber = fdData 114 Log.info(`${this.TAG} fileIO open then ${this.fdPath}`); 115 }).catch((err) => { 116 Log.info(`${this.TAG} open soundUri failed, err: ${err}`) 117 }) 118 Log.info(`${this.TAG} playSound invoke X`) 119 } 120}