/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ import {NativeModules} from 'react-native'; import {NativeJSRef, toPlainNativeJSRef} from '../NativeJSRef'; const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource'); const {PyTorchCoreAudioModule: AudioModule} = NativeModules; export interface Audio extends NativeJSRef { /** * Play an audio. */ play(): void; /** * Pause an audio. */ pause(): void; /** * Stop the current playing audio. */ stop(): void; /** * Get the duration of an audio in ms. */ getDuration(): number; /** * Until explicitly released, an [[Audio]] will have a reference in memory. * Not calling [[Audio.release]] can eventually result in an * `OutOfMemoryException`. * * :::caution * * While this is an `async` function, it does not need to be `await`ed. For * example, the `GC` on Android will eventually free the allocated memory. * * ::: */ release(): Promise; } export const wrapRef = (ref: NativeJSRef): Audio => ({ ...ref, play(): void { return AudioModule.play(ref); }, pause(): void { return AudioModule.pause(ref); }, stop(): void { return AudioModule.stop(ref); }, getDuration(): number { return AudioModule.getDuration(ref); }, async release(): Promise { return await AudioModule.release(ref); }, }); export const AudioUtil = { /** * Returns the native state of audio recording. * * ```typescript * const isRecording = await AudioUtil.isRecording(); * ``` */ isRecording(): Promise { return AudioModule.isRecording(); }, /** * Records an audio of a specific time duration. * * ```typescript * AudioUtil.startRecord(); * ``` */ startRecord(): void { AudioModule.startRecord(); }, /** * Stops an active audio recording session. * * ```typescript * const audio: Audio = await AudioUtil.stopRecord(); * ``` * * @returns A promise resolving into an [[Audio]] or null */ async stopRecord(): Promise