public class **AudioSource** extends [Object](https://developer.android.com/reference/java/lang/Object.html)  
implements [Closeable](https://developer.android.com/reference/java/io/Closeable.html) The input source (e.g. microphone, file) for audio.

### Nested Class Summary

|---|---|---|---|
| @interface | [AudioSource.Mode](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode) || The mode of the audio source. |

### Public Method Summary

|---|---|
| void | [close](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close())() Closes this audio source and releases any system resources associated with it. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromFile](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromFile(java.io.File))([File](https://developer.android.com/reference/java/io/File.html) file) Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from a local file. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromMic](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromMic())() Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from the microphone. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromPfd](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromPfd(android.os.ParcelFileDescriptor))([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd) Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html` in `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode#STREAMING` mode. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromPfd](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromPfd(android.os.ParcelFileDescriptor,%20int,%20android.media.AudioFormat))([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd, int mode, [AudioFormat](https://developer.android.com/reference/android/media/AudioFormat.html) format) Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html` with the specified `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode` and `https://developer.android.com/reference/android/media/AudioFormat.html`. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromPfd](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromPfd(android.os.ParcelFileDescriptor,%20int))([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd, int mode) Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html` with the specified `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode`. |
| static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource) | [fromUri](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromUri(android.content.Context,%20android.net.Uri))([Context](https://developer.android.com/reference/android/content/Context.html) context, [Uri](https://developer.android.com/reference/android/net/Uri.html) uri) Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource` from a Uri. |

### Inherited Method Summary

From class java.lang.Object

|---|---|
| [Object](https://developer.android.com/reference/java/lang/Object.html) | clone() |
| boolean | equals([Object](https://developer.android.com/reference/java/lang/Object.html) arg0) |
| void | finalize() |
| final [Class](https://developer.android.com/reference/java/lang/Class.html)\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| [String](https://developer.android.com/reference/java/lang/String.html) | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

From interface java.io.Closeable

|---|---|
| abstract void | close() |

From interface java.lang.AutoCloseable

|---|---|
| abstract void | close() |

## 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](https://developer.android.com/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

#### public static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromFile** ([File](https://developer.android.com/reference/java/io/File.html) file)

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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 `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close()`
on the returned `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
once inference is done to ensure the internal resources (such as the opened file
descriptor) are properly closed and cleaned up.

##### Throws

| [IOException](https://developer.android.com/reference/java/io/IOException.html) |   |
|---|---|

#### public static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromMic** ()

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromPfd** ([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd)

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
in `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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 `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromPfd(android.os.ParcelFileDescriptor,%20int)` with `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode#ONE_SHOT`, or `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromFile(java.io.File)`.

**Note:** The developer transfers ownership of the `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
to the `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`.
The developer must call `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close()`
on the returned `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
once inference is done to ensure the file descriptor is properly closed and resources
are cleaned up.

#### public static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromPfd** ([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd, int mode, [AudioFormat](https://developer.android.com/reference/android/media/AudioFormat.html) format)

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
with the specified `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource.Mode`
and `https://developer.android.com/reference/android/media/AudioFormat.html`.

### 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

- **`https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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.
- **`https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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 `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
to the `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`.
The developer must call `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close()`
on the returned `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
once inference is done to ensure the file descriptor is properly closed and resources
are cleaned up.

#### public static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromPfd** ([ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor.html) pfd, int mode)

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
from a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
with the specified `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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 `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#fromPfd(android.os.ParcelFileDescriptor,%20int,%20android.media.AudioFormat)` for details on pacing and
EOF requirements.

**Note:** The developer transfers ownership of the `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html`
to the `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`.
The developer must call `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close()`
on the returned `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
once inference is done to ensure the file descriptor is properly closed and resources
are cleaned up.

#### public static [AudioSource](https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource)
**fromUri** ([Context](https://developer.android.com/reference/android/content/Context.html) context, [Uri](https://developer.android.com/reference/android/net/Uri.html) uri)

Creates an `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/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 `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource#close()`
on the returned `https://developers.google.com/android/reference/com/google/mlkit/genai/common/audio/AudioSource`
once inference is done to ensure the internal resources (such as the opened file
descriptor) are properly closed and cleaned up.

##### Throws

| [IOException](https://developer.android.com/reference/java/io/IOException.html) |   |
|---|---|