|
| 1 | +--- |
| 2 | +title: Audio |
| 3 | +order: 50 |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The Audio API provides access to the device's microphone for recording audio. It offers a fluent interface for starting and managing recordings, tracking them with unique identifiers, and responding to completion events. |
| 9 | + |
| 10 | +```php |
| 11 | +use Native\Mobile\Facades\Audio; |
| 12 | +``` |
| 13 | + |
| 14 | +## Methods |
| 15 | + |
| 16 | +### `record()` |
| 17 | + |
| 18 | +Start an audio recording. Returns a `PendingAudioRecorder` instance that controls the recording lifecycle. |
| 19 | + |
| 20 | +```php |
| 21 | +Audio::record()->start(); |
| 22 | +``` |
| 23 | + |
| 24 | +### `stop()` |
| 25 | + |
| 26 | +Stop the current audio recording. This dispatches the `AudioRecorded` event with the recording file path. |
| 27 | + |
| 28 | +```php |
| 29 | +Audio::stop(); |
| 30 | +``` |
| 31 | + |
| 32 | +### `pause()` |
| 33 | + |
| 34 | +Pause the current audio recording without ending it. |
| 35 | + |
| 36 | +```php |
| 37 | +Audio::pause(); |
| 38 | +``` |
| 39 | + |
| 40 | +### `resume()` |
| 41 | + |
| 42 | +Resume a paused audio recording. |
| 43 | + |
| 44 | +```php |
| 45 | +Audio::resume(); |
| 46 | +``` |
| 47 | + |
| 48 | +### `getStatus()` |
| 49 | + |
| 50 | +Get the current recording status. |
| 51 | + |
| 52 | +**Returns:** `string` - One of: `"idle"`, `"recording"`, or `"paused"` |
| 53 | + |
| 54 | +```php |
| 55 | +$status = Audio::getStatus(); |
| 56 | + |
| 57 | +if ($status === 'recording') { |
| 58 | + // A recording is in progress |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### `getRecording()` |
| 63 | + |
| 64 | +Get the file path to the last recorded audio file. |
| 65 | + |
| 66 | +**Returns:** `string|null` - Path to the last recording, or `null` if none exists |
| 67 | + |
| 68 | +```php |
| 69 | +$path = Audio::getRecording(); |
| 70 | + |
| 71 | +if ($path) { |
| 72 | + // Process the recording file |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## PendingAudioRecorder |
| 77 | + |
| 78 | +The `PendingAudioRecorder` provides a fluent interface for configuring and starting audio recordings. Most methods return `$this` for method chaining. |
| 79 | + |
| 80 | +### `id(string $id)` |
| 81 | + |
| 82 | +Set a unique identifier for this recording. This ID will be included in the `AudioRecorded` event, allowing you to correlate recordings with completion events. |
| 83 | + |
| 84 | +```php |
| 85 | +$recorderId = 'message-recording-' . $this->id; |
| 86 | + |
| 87 | +Audio::record() |
| 88 | + ->id($recorderId) |
| 89 | + ->start(); |
| 90 | +``` |
| 91 | + |
| 92 | +### `getId()` |
| 93 | + |
| 94 | +Get the recorder's unique identifier. If no ID was set, one is automatically generated (UUID v4). |
| 95 | + |
| 96 | +```php |
| 97 | +$recorder = Audio::record() |
| 98 | + ->id('my-recording'); |
| 99 | + |
| 100 | +$id = $recorder->getId(); // 'my-recording' |
| 101 | +``` |
| 102 | + |
| 103 | +### `event(string $eventClass)` |
| 104 | + |
| 105 | +Set a custom event class to dispatch when recording completes. By default, `AudioRecorded` is used. |
| 106 | + |
| 107 | +**Throws:** `InvalidArgumentException` if the event class does not exist |
| 108 | + |
| 109 | +```php |
| 110 | +use App\Events\VoiceMessageRecorded; |
| 111 | + |
| 112 | +Audio::record() |
| 113 | + ->event(VoiceMessageRecorded::class) |
| 114 | + ->start(); |
| 115 | +``` |
| 116 | + |
| 117 | +### `remember()` |
| 118 | + |
| 119 | +Store the recorder's ID in the session for later retrieval. This is useful when the recording completes on the next request. |
| 120 | + |
| 121 | +```php |
| 122 | +Audio::record() |
| 123 | + ->id('voice-note') |
| 124 | + ->remember() |
| 125 | + ->start(); |
| 126 | +``` |
| 127 | + |
| 128 | +### `lastId()` |
| 129 | + |
| 130 | +Retrieve the last remembered audio recorder ID from the session. Use this in event listeners to correlate recordings. |
| 131 | + |
| 132 | +```php |
| 133 | +use Livewire\Attributes\On; |
| 134 | +use Native\Mobile\Events\Audio\AudioRecorded; |
| 135 | + |
| 136 | +#[On('native:'.AudioRecorded::class)] |
| 137 | +public function handleAudioRecorded(string $path, string $mimeType, ?string $id) |
| 138 | +{ |
| 139 | + // For comparing with remembered IDs |
| 140 | + if ($id === Audio::record()->lastId()) { |
| 141 | + $this->saveRecording($path); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### `start()` |
| 147 | + |
| 148 | +Explicitly start the audio recording. This is optional - recordings auto-start if you don't call this method. |
| 149 | + |
| 150 | +**Returns:** `bool` - `true` if recording started successfully, `false` if it failed or was already started |
| 151 | + |
| 152 | +```php |
| 153 | +$recorder = Audio::record()->id('my-recording'); |
| 154 | + |
| 155 | +if ($recorder->start()) { |
| 156 | + // Recording started |
| 157 | +} else { |
| 158 | + // Recording failed - likely due to permission denial |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Events |
| 163 | + |
| 164 | +### `AudioRecorded` |
| 165 | + |
| 166 | +Dispatched when an audio recording completes. The event includes the file path and recording ID. |
| 167 | + |
| 168 | +**Payload:** |
| 169 | +- `string $path` - File path to the recorded audio |
| 170 | +- `string $mimeType` - MIME type of the audio (default: `'audio/m4a'`) |
| 171 | +- `?string $id` - The recorder's ID, if one was set |
| 172 | + |
| 173 | +```php |
| 174 | +use Livewire\Attributes\On; |
| 175 | +use Native\Mobile\Events\Audio\AudioRecorded; |
| 176 | + |
| 177 | +#[On('native:'.AudioRecorded::class)] |
| 178 | +public function handleAudioRecorded(string $path, string $mimeType, ?string $id) |
| 179 | +{ |
| 180 | + // Store or process the recording |
| 181 | + $this->recordings[] = [ |
| 182 | + 'path' => $path, |
| 183 | + 'mimeType' => $mimeType, |
| 184 | + 'id' => $id, |
| 185 | + ]; |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +## Notes |
| 190 | + |
| 191 | +- **Microphone Permission:** The first time your app requests microphone access, users will be prompted for permission. If denied, recording functions will fail silently. |
| 192 | + |
| 193 | +- **File Format:** Recordings are stored as M4A/AAC audio files (`.m4a`). This format is optimized for small file sizes while maintaining quality. |
| 194 | + |
| 195 | +- **Storage Location:** |
| 196 | + - **Android:** Recordings are stored in the app's cache directory (`context.cacheDir/audio_{timestamp}.m4a`). These are temporary files and may be deleted by the system. |
| 197 | + - **iOS:** Recordings are stored persistently in `~/Library/Application Support/Audio/NativePHP_{timestamp}.m4a` and are excluded from iCloud backup. |
| 198 | + |
| 199 | +- **Recording State:** Only one recording can be active at a time. Calling `start()` while a recording is in progress will return `false`. |
| 200 | + |
| 201 | +- **Auto-Start Behavior:** If you don't explicitly call `start()`, the recording will automatically start when the `PendingAudioRecorder` is destroyed. This maintains backward compatibility with earlier versions. |
0 commit comments