|
| 1 | +# AVPlayer |
| 2 | + |
| 3 | +The `AVPlayer` API allows you to play audio or video with various customizable options. Below is a detailed guide on how to use this API effectively, with notes on best practices and potential issues. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +To use `AVPlayer`, create an instance and set the media source using `setSource()`. Configure the desired playback options like `volume` or `rate`, and start playback with `play()`. |
| 8 | + |
| 9 | +```typescript |
| 10 | +const player = new AVPlayer(); |
| 11 | + |
| 12 | +// Set the media source (local or remote) |
| 13 | +if (player.setSource("https://example.com/audio.mp3")) { |
| 14 | + player.onReadyToPlay = () => { |
| 15 | + player.play(); |
| 16 | + }; |
| 17 | + player.onEnded = () => { |
| 18 | + console.log("Playback finished."); |
| 19 | + }; |
| 20 | +} else { |
| 21 | + console.error("Failed to set media source."); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## API Reference |
| 28 | + |
| 29 | +### Properties |
| 30 | + |
| 31 | +- **`volume`**: `number` |
| 32 | + |
| 33 | + - Controls the playback volume, ranging from `0.0` (muted) to `1.0` (full volume). |
| 34 | + - Example: |
| 35 | + ```typescript |
| 36 | + player.volume = 0.5; // Set to 50% volume |
| 37 | + ``` |
| 38 | + |
| 39 | +- **`duration`**: `DurationInSeconds` |
| 40 | + |
| 41 | + - The total duration of the media. Will be `0` until the media is ready. |
| 42 | + - Example: |
| 43 | + ```typescript |
| 44 | + console.log(`Media duration: ${player.duration} seconds`); |
| 45 | + ``` |
| 46 | + |
| 47 | +- **`currentTime`**: `DurationInSeconds` |
| 48 | + |
| 49 | + - The current playback position. Can also be set to seek to a specific time. |
| 50 | + - Example: |
| 51 | + ```typescript |
| 52 | + player.currentTime = 30; // Seek to 30 seconds |
| 53 | + ``` |
| 54 | + |
| 55 | +- **`rate`**: `number` |
| 56 | + |
| 57 | + - Controls playback speed. `1.0` is normal speed; higher values increase speed. |
| 58 | + - Example: |
| 59 | + ```typescript |
| 60 | + player.rate = 1.5; // Play 1.5x faster |
| 61 | + ``` |
| 62 | + |
| 63 | +- **`timeControlStatus`**: `TimeControlStatus` |
| 64 | + |
| 65 | + - Indicates playback state (e.g., playing, paused, waiting). |
| 66 | + |
| 67 | +- **`numberOfLoops`**: `number` |
| 68 | + - Sets how many times the media should loop. `0` for no loops, `-1` for infinite loops. |
| 69 | + - Example: |
| 70 | + ```typescript |
| 71 | + player.numberOfLoops = -1; // Loop indefinitely |
| 72 | + ``` |
| 73 | + |
| 74 | +### Methods |
| 75 | + |
| 76 | +- **`setSource(filePathOrURL: string): boolean`** |
| 77 | + |
| 78 | + - Sets the media source. Accepts a local file path or a remote URL. |
| 79 | + - Returns `true` on success. |
| 80 | + |
| 81 | +- **`play(): boolean`** |
| 82 | + |
| 83 | + - Starts playback. Returns `true` if successful. |
| 84 | + |
| 85 | +- **`pause()`** |
| 86 | + |
| 87 | + - Pauses playback. |
| 88 | + |
| 89 | +- **`stop()`** |
| 90 | + |
| 91 | + - Stops playback and resets to the beginning. |
| 92 | + |
| 93 | +- **`dispose()`** |
| 94 | + - Releases all resources. Must be called when the player is no longer needed. |
| 95 | + |
| 96 | +### Callbacks |
| 97 | + |
| 98 | +- **`onReadyToPlay?: () => void`** |
| 99 | + |
| 100 | + - Called when the media is ready to play. |
| 101 | + |
| 102 | +- **`onTimeControlStatusChanged?: (status: TimeControlStatus) => void`** |
| 103 | + |
| 104 | + - Called when the playback status changes. |
| 105 | + |
| 106 | +- **`onEnded?: () => void`** |
| 107 | + |
| 108 | + - Called when playback ends. |
| 109 | + |
| 110 | +- **`onError?: (message: string) => void`** |
| 111 | + - Called when an error occurs. |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Handling Audio Sessions |
| 116 | + |
| 117 | +The `AVPlayer` API relies on the system's shared audio session. Here are key considerations: |
| 118 | + |
| 119 | +1. **Audio Session Interruption** |
| 120 | + |
| 121 | + - Register an interruption listener using `SharedAudioSession.addInterruptionListener` to handle interruptions such as incoming calls. |
| 122 | + - Example: |
| 123 | + ```typescript |
| 124 | + SharedAudioSession.addInterruptionListener((type) => { |
| 125 | + if (type === "began") { |
| 126 | + player.pause(); |
| 127 | + } else if (type === "ended") { |
| 128 | + player.play(); |
| 129 | + } |
| 130 | + }); |
| 131 | + ``` |
| 132 | + |
| 133 | +2. **Category and Options** |
| 134 | + |
| 135 | + - Set the appropriate audio session category and options based on the use case. |
| 136 | + - Example: |
| 137 | + ```typescript |
| 138 | + await SharedAudioSession.setCategory("playback", ["mixWithOthers"]); |
| 139 | + ``` |
| 140 | + |
| 141 | +3. **Activation** |
| 142 | + - Activate the audio session before playback. |
| 143 | + - Example: |
| 144 | + ```typescript |
| 145 | + await SharedAudioSession.setActive(true); |
| 146 | + ``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## Common Use Cases |
| 151 | + |
| 152 | +### Play Remote Audio |
| 153 | + |
| 154 | +```typescript |
| 155 | +player.setSource("https://example.com/audio.mp3"); |
| 156 | +player.onReadyToPlay = () => { |
| 157 | + player.play(); |
| 158 | +}; |
| 159 | +``` |
| 160 | + |
| 161 | +### Play Local Audio |
| 162 | + |
| 163 | +```typescript |
| 164 | +player.setSource("/path/to/local/audio.mp3"); |
| 165 | +player.play(); |
| 166 | +``` |
| 167 | + |
| 168 | +### Handle Playback Events |
| 169 | + |
| 170 | +```typescript |
| 171 | +player.onEnded = () => { |
| 172 | + console.log("Playback finished."); |
| 173 | +}; |
| 174 | +player.onError = (message) => { |
| 175 | + console.error("Playback error:", message); |
| 176 | +}; |
| 177 | +``` |
| 178 | + |
| 179 | +### Loop Playback |
| 180 | + |
| 181 | +```typescript |
| 182 | +player.numberOfLoops = 3; // Loop 3 times |
| 183 | +player.play(); |
| 184 | +``` |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Best Practices |
| 189 | + |
| 190 | +1. **Resource Management** |
| 191 | + |
| 192 | + - Always call `dispose()` when the player is no longer needed. |
| 193 | + |
| 194 | +2. **Error Handling** |
| 195 | + |
| 196 | + - Implement `onError` to handle playback issues, such as network failures. |
| 197 | + |
| 198 | +3. **Audio Session Configuration** |
| 199 | + |
| 200 | + - Configure the audio session category and options appropriately for your app's behavior. |
| 201 | + |
| 202 | +4. **Interruption Management** |
| 203 | + |
| 204 | + - Use interruption listeners to pause and resume playback gracefully. |
| 205 | + |
| 206 | +5. **Optimize for User Experience** |
| 207 | + - Provide UI feedback for loading and playback states using `onReadyToPlay` and `onTimeControlStatusChanged` callbacks. |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## Full Example |
| 212 | + |
| 213 | +Here is a complete example showcasing how to use the `AVPlayer` API: |
| 214 | + |
| 215 | +```typescript |
| 216 | +// Create an instance of AVPlayer |
| 217 | +const player = new AVPlayer(); |
| 218 | +
|
| 219 | +// Configure audio session |
| 220 | +await SharedAudioSession.setCategory("playback", ["mixWithOthers"]); |
| 221 | +await SharedAudioSession.setActive(true); |
| 222 | +
|
| 223 | +// Set the media source |
| 224 | +if (player.setSource("https://example.com/audio.mp3")) { |
| 225 | + // Register events |
| 226 | + player.onReadyToPlay = () => { |
| 227 | + console.log("Media is ready to play"); |
| 228 | + player.play(); |
| 229 | + }; |
| 230 | +
|
| 231 | + player.onEnded = () => { |
| 232 | + console.log("Playback finished"); |
| 233 | + player.dispose(); |
| 234 | + }; |
| 235 | +
|
| 236 | + player.onError = (message) => { |
| 237 | + console.error("Playback error:", message); |
| 238 | + player.dispose(); |
| 239 | + }; |
| 240 | +} else { |
| 241 | + console.error("Failed to set media source"); |
| 242 | +} |
| 243 | +
|
| 244 | +// Handle interruptions |
| 245 | +SharedAudioSession.addInterruptionListener((type) => { |
| 246 | + if (type === "began") { |
| 247 | + player.pause(); |
| 248 | + } else if (type === "ended") { |
| 249 | + player.play(); |
| 250 | + } |
| 251 | +}); |
| 252 | +``` |
0 commit comments