AudioSource

public class AudioSource extends Object
implements Closeable

The input source (e.g. microphone, file) for audio.

Nested Class Summary

@interface AudioSource.Mode The mode of the audio source. 

Public Method Summary

void
close()
Closes this audio source and releases any system resources associated with it.
static AudioSource
fromFile(File file)
Creates an AudioSource from a local file.
static AudioSource
fromMic()
Creates an AudioSource from the microphone.
static AudioSource
static AudioSource
fromPfd(ParcelFileDescriptor pfd, int mode, AudioFormat format)
Creates an AudioSource from a ParcelFileDescriptor with the specified AudioSource.Mode and AudioFormat.
static AudioSource
fromPfd(ParcelFileDescriptor pfd, int mode)
Creates an AudioSource from a ParcelFileDescriptor with the specified AudioSource.Mode.
static AudioSource
fromUri(Context context, Uri uri)
Creates an AudioSource from a Uri.

Inherited Method Summary

Public Methods

public void close ()

Closes this audio source and releases any system resources associated with it.

If the source is already closed, then invoking this method has no effect.

Throws
IOException if an I/O error occurs.

public static AudioSource fromFile (File file)

Creates an AudioSource from a local file.

The SDK will attempt to auto-detect the format from the file extension. It supports raw formats (e.g. .raw, .pcm) and common container formats (e.g. .mp3, .wav, .flac, .m4a, .ogg) which will be decoded automatically on the device.

If the format is invalid or unsupported, an error will be returned when starting recognition.

Note: The developer must call close() on the returned AudioSource once inference is done to ensure the internal resources (such as the opened file descriptor) are properly closed and cleaned up.

Throws
IOException

public static AudioSource fromMic ()

Creates an AudioSource from the microphone.

Note: This API requires the android.permission.RECORD_AUDIO permission. Make sure to declare it in your AndroidManifest.xml:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
 

public static AudioSource fromPfd (ParcelFileDescriptor pfd)

Creates an AudioSource from a ParcelFileDescriptor in AudioSource.Mode.STREAMING mode.

The input must be raw, headerless audio data in a mono (single channel), 16-bit PCM format with a 16 kHz sample rate.

Furthermore, the file descriptor must supply data at a real-time pace (e.g., producing approximately 32,000 bytes of new data every second). For pre-recorded audio files or batch audio where pacing is not needed, use fromPfd(ParcelFileDescriptor, int) with AudioSource.Mode.ONE_SHOT, or fromFile(File).

Note: The developer transfers ownership of the ParcelFileDescriptor to the AudioSource. The developer must call close() on the returned AudioSource once inference is done to ensure the file descriptor is properly closed and resources are cleaned up.

public static AudioSource fromPfd (ParcelFileDescriptor pfd, int mode, AudioFormat format)

Creates an AudioSource from a ParcelFileDescriptor with the specified AudioSource.Mode and AudioFormat.

Audio Format Requirements

The speech recognizer typically requires raw, headerless PCM audio data. The recommended format is 16-bit PCM, mono (single channel), with a 16 kHz sample rate.

Pacing and EOF Contracts

  • AudioSource.Mode.STREAMING: The audio data must be written to the pfd at a rate matching real-time playback (e.g., 32,000 bytes per second for 16 kHz, 16-bit, mono PCM). Writing data too quickly may overwhelm the buffer, and writing too slowly will delay recognition results.
  • AudioSource.Mode.ONE_SHOT: You can write the entire audio dataset as fast as possible. However, you must close the write end of the file descriptor to signal End-of-File (EOF). The recognizer will not deliver the final result until EOF is received.

Note: The developer transfers ownership of the ParcelFileDescriptor to the AudioSource. The developer must call close() on the returned AudioSource once inference is done to ensure the file descriptor is properly closed and resources are cleaned up.

public static AudioSource fromPfd (ParcelFileDescriptor pfd, int mode)

Creates an AudioSource from a ParcelFileDescriptor with the specified AudioSource.Mode.

The input must be raw, headerless audio data in a mono (single channel), 16-bit PCM format with a 16 kHz sample rate.

Pacing and EOF Contracts: Refer to fromPfd(ParcelFileDescriptor, int, AudioFormat) for details on pacing and EOF requirements.

Note: The developer transfers ownership of the ParcelFileDescriptor to the AudioSource. The developer must call close() on the returned AudioSource once inference is done to ensure the file descriptor is properly closed and resources are cleaned up.

public static AudioSource fromUri (Context context, Uri uri)

Creates an AudioSource from a Uri.

The SDK will attempt to auto-detect the format from the content type or URI path. It supports raw formats (e.g. .raw, .pcm extensions or equivalent MIME types) and common container formats (e.g. .mp3, .wav, .flac, .m4a, .ogg extensions or equivalent MIME types) which will be decoded automatically on the device.

If the format is invalid or unsupported, an error will be returned when starting recognition.

Note: The developer must call close() on the returned AudioSource once inference is done to ensure the internal resources (such as the opened file descriptor) are properly closed and cleaned up.

Throws
IOException