|
| 1 | +# Media Control API |
| 2 | + |
| 3 | +Our [Media Control API](../src/api/media/MediaControlAPI.ts) provides a unified way to customise the behaviour of the different media playback controls and media sessions across platforms (iOS, Android, and Web). It enables integration with platform-level media controls such as lock screen controls, notification controls, media session, ... |
| 4 | + |
| 5 | +## What is it used for? |
| 6 | + |
| 7 | +- **Remote Control Actions:** Handle play, pause, seek, skip, and track switching from system UI (lock screen, notifications, etc.). |
| 8 | +- **Media Session Integration:** Display media state and respond to hardware/media key events. |
| 9 | +- **Custom Playlist Navigation:** Enable playlist navigation using system controls. |
| 10 | + |
| 11 | +## Platform Support |
| 12 | + |
| 13 | +- **Android:** Integrates with [Media Session](https://developer.android.com/guide/topics/media-apps/working-with-a-media-session) and media notifications. |
| 14 | +- **iOS:** Integrates with [Now Playing](https://developer.apple.com/documentation/mediaplayer/mpnowplayinginfocenter) and [Remote Command Center](https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter). |
| 15 | +- **Web:** Integrates with the [Media Session API](https://www.w3.org/TR/mediasession/). |
| 16 | + |
| 17 | +## MediaControl API and MediaControl Action Reference |
| 18 | + |
| 19 | +The [Media Control API](../src/api/media/MediaControlAPI.ts) allows you to override the default player's behaviour, by defining a handler for one of the MediaControl Actions: |
| 20 | +```typescript |
| 21 | +setHandler(action: MediaControlAction, handler: MediaControlHandler): void; |
| 22 | +``` |
| 23 | + |
| 24 | +The [MediaControlAction](../src/api/media/MediaControlAPI.ts) enum defines all actions that can be controlled by the Media Control API: |
| 25 | + |
| 26 | +- `PLAY`: Triggered when the user presses play. |
| 27 | +- `PAUSE`: Triggered when the user presses pause. |
| 28 | +- `SEEK_FORWARD`: Triggered when the user requests to seek forward by a preset interval. |
| 29 | +- `SEEK_BACKWARD`: Triggered when the user requests to seek backward by a preset interval. |
| 30 | +- `SKIP_TO_NEXT`: Triggered when the user requests to go to the next track or playlist item. |
| 31 | +- `SKIP_TO_PREVIOUS`: Triggered when the user requests to go to the previous track or playlist item. |
| 32 | + |
| 33 | +Play and pause are only enabled for VOD and when the stream is not displaying an ad. For LIVE streams this can be configured through `allowLivePlayPause` in the player's [MediaControlConfiguration](../src/api/media/MediaControlConfiguration.ts). |
| 34 | + |
| 35 | +If no handler is defined for an action, the player's default behaviour is applied. |
| 36 | + |
| 37 | +### iOS: Track Control vs. Seek Behavior |
| 38 | + |
| 39 | +On iOS, when you set handlers for `SKIP_TO_NEXT` or `SKIP_TO_PREVIOUS`, these handlers will take precedence over the seek behavior. This means: |
| 40 | + |
| 41 | +- **If you provide next/previous track handlers:** |
| 42 | + - System controls (e.g., lock screen) will display and trigger your handlers for track navigation. |
| 43 | + - Seeking forward/backward via next/previous is not shown as seperate controls. (Platform limitation) |
| 44 | +- **If you do not provide next/previous handlers:** |
| 45 | + - The system will display and use the default or configured seek forward/backward functionality. |
| 46 | + |
| 47 | +This allows you to customize whether system controls are used for playlist navigation or for seeking within the current track. |
| 48 | + |
| 49 | +Note: In both cases, you can always use the system's time slider to adjust the playhead to seek to a location in the stream. |
| 50 | + |
| 51 | +## Configuration |
| 52 | + |
| 53 | +You can add additional media control configuration using the [MediaControlConfiguration](../src/api/media/MediaControlConfiguration.ts) interface: |
| 54 | + |
| 55 | +```typescript |
| 56 | +export interface MediaControlConfiguration { |
| 57 | + mediaSessionEnabled?: boolean; // (Web/Android) Enable/disable media session (default: true) |
| 58 | + skipForwardInterval?: number; // (Web/Android/iOS) Skip forward interval (defaults: 5s on Web, Android / 15s on iOS) |
| 59 | + skipBackwardInterval?: number; // (Web/Android/iOS) Skip backward interval (defaults: 5s on Web, Android / 15s on iOS) |
| 60 | + allowLivePlayPause?: boolean; // (Android/iOS) Enable play/pause for live (defaults: false on Android / true on iOS) |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Example Usage: Playlist Navigation |
| 65 | + |
| 66 | +The Media Control API can be used to handle playlist navigation via system controls. For example, in a custom React hook: |
| 67 | + |
| 68 | +```typescript |
| 69 | +import { MediaControlAction } from 'react-native-theoplayer'; |
| 70 | + |
| 71 | +// ... |
| 72 | +useEffect(() => { |
| 73 | + if (!player) return; |
| 74 | + |
| 75 | + const handleNext = () => { /* update player source ... */ }; |
| 76 | + const handlePrevious = () => { /* update player source ... */ }; |
| 77 | + |
| 78 | + player.mediaControl?.setHandler(MediaControlAction.SKIP_TO_NEXT, handleNext); |
| 79 | + player.mediaControl?.setHandler(MediaControlAction.SKIP_TO_PREVIOUS, handlePrevious); |
| 80 | +}, [player, filteredSources]); |
| 81 | +``` |
| 82 | + |
| 83 | +This enables users to skip tracks using lock screen or Bluetooth controls. If you do not set these handlers, the controls will perform seek actions instead. |
| 84 | + |
| 85 | +## Demo |
| 86 | + |
| 87 | +As a demonstration, see the the `usePlaylist` hook in our [example app](../example/). |
0 commit comments