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 worker from '@ohos.worker'; 17import { HiLog } from '../../../../../common/src/main/ets/util/HiLog'; 18 19const TAG = "AudioWorker "; 20 21export class AudioHepler { 22 private workerName: string; 23 private workerUri: string; 24 private mWorker; 25 constructor(name: string, workerUri: string) { 26 this.workerName = name; 27 this.workerUri = workerUri; 28 this.initWorker(); 29 } 30 31 private initWorker(): void { 32 this.mWorker = new worker.Worker(this.workerUri, { type: "classic", name: this.workerName }); 33 this.mWorker.onerror = this.onError.bind(this); 34 this.mWorker.onmessageError = this.onMessageError.bind(this); 35 this.mWorker.onexit = this.onExit.bind(this); 36 this.mWorker.onmessage = this.onMessage.bind(this); 37 } 38 39 public onError(e: any): void { 40 HiLog.e(TAG, "AudioWorker is error" + JSON.stringify(e)); 41 } 42 43 public onMessageError(e: any): void { 44 HiLog.e(TAG, "AudioWorker Message is error" + JSON.stringify(e)); 45 } 46 47 public onExit(e: any): void { 48 HiLog.e(TAG, "AudioWorker is Exit"); 49 } 50 51 public onMessage(e: any): void { 52 HiLog.e(TAG, "AudioWorker return message"); 53 } 54 55 public postMessage(message: any): void { 56 HiLog.i(TAG, "AudioWorker postMessage"); 57 this.mWorker.postMessage(message); 58 } 59}