@irvingouj/expo-audio-stream
Version:
Expo Play Audio Stream module - Fork of expo-audio-stream with additional features and Expo SDK 52+ support
179 lines • 9.13 kB
TypeScript
import { EventSubscription } from 'expo-modules-core';
import { AudioDataEvent, RecordingAudioDataEvent, MicrophoneAudioDataEvent, AudioRecording, RecordingConfig, MicrophoneConfig, StartRecordingResult, StartMicrophoneResult, SoundConfig, PlaybackMode, Encoding, EncodingTypes, PlaybackModes } from "./types";
import { SoundChunkPlayedEventPayload, AudioEvents, DeviceReconnectedReason, DeviceReconnectedEventPayload } from "./events";
declare const SuspendSoundEventTurnId = "suspend-sound-events";
export declare class ExpoPlayAudioStream {
/**
* Destroys the audio stream module, cleaning up all resources.
* This should be called when the module is no longer needed.
* It will reset all internal state and release audio resources.
*/
static destroy(): void;
/**
* Starts audio recording to file with volume feedback.
* @param {RecordingConfig} recordingConfig - Configuration for the recording.
* @returns {Promise<{recordingResult: StartRecordingResult, subscription: EventSubscription | undefined}>} A promise that resolves to an object containing the recording result and a subscription to volume events.
* @throws {Error} If the recording fails to start.
* @note Records audio to an M4A file and emits only volume levels (not raw audio data) via AudioData events.
*/
static startRecording(recordingConfig: RecordingConfig): Promise<{
recordingResult: StartRecordingResult;
subscription?: EventSubscription;
}>;
/**
* Stops the current microphone recording.
* @returns {Promise<AudioRecording>} A promise that resolves to the audio recording data.
* @throws {Error} If the recording fails to stop.
*/
static stopRecording(): Promise<AudioRecording>;
/**
* Plays an audio chunk.
* @param {string} base64Chunk - The base64 encoded audio chunk to play.
* @param {string} turnId - The turn ID.
* @param {string} [encoding] - The encoding format of the audio data ('pcm_f32le' or 'pcm_s16le').
* @returns {Promise<void>}
* @throws {Error} If the audio chunk fails to stream.
*/
static playAudio(base64Chunk: string, turnId: string, encoding?: Encoding): Promise<void>;
/**
* Pauses the current audio playback.
* @returns {Promise<void>}
* @throws {Error} If the audio playback fails to pause.
*/
static pauseAudio(): Promise<void>;
/**
* Stops the currently playing audio.
* @returns {Promise<void>}
* @throws {Error} If the audio fails to stop.
*/
static stopAudio(): Promise<void>;
/**
* Clears the playback queue by turn ID.
* @param {string} turnId - The turn ID.
* @returns {Promise<void>}
* @throws {Error} If the playback queue fails to clear.
*/
static clearPlaybackQueueByTurnId(turnId: string): Promise<void>;
/**
* Plays a sound.
* @param {string} audio - The audio to play.
* @param {string} turnId - The turn ID.
* @param {string} [encoding] - The encoding format of the audio data ('pcm_f32le' or 'pcm_s16le').
* @returns {Promise<void>}
* @throws {Error} If the sound fails to play.
*/
static playSound(audio: string, turnId: string, encoding?: Encoding): Promise<void>;
/**
* Stops the currently playing sound.
* @returns {Promise<void>}
* @throws {Error} If the sound fails to stop.
*/
static stopSound(): Promise<void>;
/**
* Interrupts the current sound.
* @returns {Promise<void>}
* @throws {Error} If the sound fails to interrupt.
*/
static interruptSound(): Promise<void>;
/**
* Resumes the current sound.
* @returns {Promise<void>}
* @throws {Error} If the sound fails to resume.
*/
static resumeSound(): void;
/**
* Clears the sound queue by turn ID.
* @param {string} turnId - The turn ID.
* @returns {Promise<void>}
* @throws {Error} If the sound queue fails to clear.
*/
static clearSoundQueueByTurnId(turnId: string): Promise<void>;
/**
* Starts microphone streaming for real-time audio data.
* @param {MicrophoneConfig} microphoneConfig - The microphone streaming configuration.
* @returns {Promise<{recordingResult: StartMicrophoneResult, subscription: EventSubscription | undefined}>} A promise that resolves to an object containing the recording result and a subscription to audio data events.
* @throws {Error} If the microphone streaming fails to start.
*/
static startMicrophone(microphoneConfig: MicrophoneConfig): Promise<{
recordingResult: StartMicrophoneResult;
subscription?: EventSubscription;
}>;
/**
* Stops the current microphone streaming.
* @returns {Promise<void>}
* @throws {Error} If the microphone streaming fails to stop.
*/
static stopMicrophone(): Promise<AudioRecording | null>;
/**
* Subscribes to audio events emitted during recording/streaming.
* @param onMicrophoneStream - Callback function that will be called when audio data is received.
* The callback receives an AudioDataEvent containing:
* - data: For recording: empty data (volume feedback only). For streaming: base64 encoded audio data
* - position: Current position in the audio stream
* - fileUri: URI of the recording file (empty for streaming)
* - eventDataSize: Size of the current audio data chunk (0 for recording volume events)
* - totalSize: Total size of recorded audio so far (0 for recording volume events)
* - soundLevel: Volume level in dBFS (-160.0 to 0.0)
* @returns {EventSubscription} A subscription object that can be used to unsubscribe from the events
* @note For file recording, only soundLevel contains meaningful data. For streaming, all fields are populated.
*/
static subscribeToAudioEvents(onAudioStream: (event: AudioDataEvent) => Promise<void>): EventSubscription;
/**
* Subscribes to events emitted when a sound chunk has finished playing.
* @param onSoundChunkPlayed - Callback function that will be called when a sound chunk is played.
* The callback receives a SoundChunkPlayedEventPayload indicating if this was the final chunk.
* @returns {EventSubscription} A subscription object that can be used to unsubscribe from the events.
*/
static subscribeToSoundChunkPlayed(onSoundChunkPlayed: (event: SoundChunkPlayedEventPayload) => Promise<void>): EventSubscription;
/**
* Subscribes to events emitted by the audio stream module, for advanced use cases.
* @param eventName - The name of the event to subscribe to.
* @param onEvent - Callback function that will be called when the event is emitted.
* @returns {EventSubscription} A subscription object that can be used to unsubscribe from the events.
*/
static subscribe<T extends unknown>(eventName: string, onEvent: (event: T | undefined) => Promise<void>): EventSubscription;
/**
* Plays a WAV audio file from base64 encoded data.
* Unlike playSound(), this method plays the audio directly without queueing.
* @param {string} wavBase64 - Base64 encoded WAV audio data.
* @returns {Promise<void>}
* @throws {Error} If the WAV audio fails to play.
*/
static playWav(wavBase64: string): Promise<void>;
/**
* Plays an M4A audio file from base64 encoded data.
* Unlike playSound(), this method plays the audio directly without queueing.
* @param m4aBase64 - Base64 encoded M4A audio data.
* @returns Promise that resolves when playback starts.
* @throws Error if the M4A audio fails to play.
*/
static playM4a(m4aBase64: string): Promise<void>;
/**
* Plays an M4A audio file from file path.
* @param fileUri - File URI to the M4A file.
* @returns Promise that resolves when playback starts.
* @throws Error if the M4A file fails to play.
*/
static playM4aFile(fileUri: string): Promise<void>;
/**
* Sets the sound player configuration.
* @param {SoundConfig} config - Configuration options for the sound player.
* @returns {Promise<void>}
* @throws {Error} If the configuration fails to update.
*/
static setSoundConfig(config: SoundConfig): Promise<void>;
/**
* Prompts the user to select the microphone mode.
* @returns {Promise<void>}
* @throws {Error} If the microphone mode fails to prompt.
*/
static promptMicrophoneModes(): void;
/**
* Toggles the silence state of the microphone.
* @returns {Promise<void>}
* @throws {Error} If the microphone fails to toggle silence.
*/
static toggleSilence(): void;
}
export { AudioDataEvent, RecordingAudioDataEvent, MicrophoneAudioDataEvent, SoundChunkPlayedEventPayload, DeviceReconnectedReason, DeviceReconnectedEventPayload, AudioRecording, RecordingConfig, MicrophoneConfig, StartRecordingResult, StartMicrophoneResult, AudioEvents, SuspendSoundEventTurnId, SoundConfig, PlaybackMode, Encoding, EncodingTypes, PlaybackModes, };
//# sourceMappingURL=index.d.ts.map