1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 */ 15import worker, { MessageEvents } from '@ohos.worker'; 16import AudioRendererHelper from '../audio/AudioRendererHelper'; 17import Constants from '../Constants'; 18import Logger from '../utils/Logger'; 19 20let parent = worker.workerPort; 21let mRenderer: AudioRendererHelper = new AudioRendererHelper(); 22 23// 处理来自主线程的消息 24parent.onmessage = (message:MessageEvents) => { 25 Logger.info(`mAudioRenderer onmessage: ${message.data.code}`); 26 let messageId: number = message.data.code; 27 if (messageId === Constants.WORK_MESSAGE_RENDERER_START) { 28 //开始录音 29 start(); 30 } else if (messageId === Constants.WORK_MESSAGE_RENDERER_STOP) { 31 //结束录音 32 stop(); 33 } else if (messageId === Constants.WORK_MESSAGE_RENDERER_RECV) { 34 //渲染 35 let buffer: ArrayBuffer = message.data.buffer; 36 mRenderer.write(buffer); 37 } 38}; 39 40async function start(): Promise<void> { 41 await mRenderer.init(); 42 await mRenderer.start(); 43} 44 45async function stop(): Promise<void> { 46 await mRenderer.stop(); 47 await mRenderer.release(); 48}