• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AudioCapturerHelper from '../audio/AudioCapturerHelper';
17import Constants from '../Constants';
18import Logger from '../utils/Logger';
19
20let parent = worker.workerPort;
21let mCapturer: AudioCapturerHelper = new AudioCapturerHelper();
22
23// 处理来自主线程的消息
24parent.onmessage = (message: MessageEvents) => {
25  Logger.info(`mAudioCapturer message from worker: ${message.data.code}`);
26
27  let messageId: number = message.data.code;
28  if (messageId === Constants.WORK_MESSAGE_CAPTURER_START) {
29    //开始录音
30    start();
31  } else if (messageId === Constants.WORK_MESSAGE_CAPTURER_STOP) {
32    //结束录音
33    stop();
34  }
35};
36
37async function start(): Promise<void> {
38  await mCapturer.init();
39  await mCapturer.start((buffer: ArrayBuffer, size: number) => {
40    parent.postMessage({
41      'code': Constants.WORK_MESSAGE_CAPTURER_SEND,
42      'buffer': buffer
43    }, [buffer]);
44  });
45}
46
47async function stop(): Promise<void> {
48  await mCapturer.stop();
49  await mCapturer.release();
50}
51
52