Skip to main content
Version: 10.9.0

Background Playback and Notifications

Overview

One of the key features of react-native-theoplayer is its support for background playback, notifications, and lock-screen controls. This feature allows your users to continue listening to audio when the app is not in the foreground, and control playback using notifications and lock-screen controls.

Configuration

The backgroundAudioConfiguration player configuration affects the play-out behaviour when the app is moved to the background: in case the enabled property is set to true, the current media asset will continue playing when the app is moved to the background, otherwise play-out will pause.

The configuration can be modified at run-time, allowing the player to pause depending on the current media asset's properties, or whether an ad is playing or not.

player.backgroundAudioConfiguration = { enabled: true };

Other properties of the backgroundAudioConfiguration can be used to further customize the background playback:

FieldPlatformPurposeDefault
enabledWeb, iOS & AndroidIf enabled, playback continues when transitioning to background.false
stopOnBackgroundWeb, iOS & AndroidIf enabled, fully stop playback when the app goes to the background. The player will need to be provided with a new source when the app comes back to the foreground.false
shouldResumeAfterInterruptioniOSControls whether playback should automatically resume after an interruption, such as an incoming phone call.false
audioSessionModeiOSSpecifies the AVAudioSession.ModeAudioSessionMode.MOVIE_PLAYBACK

A use case for enabling stopOnBackground could be a live stream that should not be allowed to continue in the background, neither in paused nor playing state. The app should be designed to reload the source or the player page when coming back to the foreground.

Android

On Android, a service is used to continue playback in the background while the user is not interacting with the app.

It is possible to disable the service at build time by setting the build config field 'THEOplayer_usePlaybackService = false'. In that case the background playback feature is always disabled, no notifications are displayed, and the player will always pause when the hosting app goes to the background.

Notifications, Metadata and Lockscreen Controls

During play-out of a media asset, a notification is displayed that provides some metadata and enables basic control. The source description passed to the player should provide the necessary metadata properties:

const source = {
// ...
poster: 'https://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg',
metadata: {
title: 'My stream title',
subtitle: 'A subtitle or artist',
nowPlayingServiceIdentifier: 'serviceId',
nowPlayingContentIdentifier: 'contentId',
displayIconUri: 'https://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg',
}
}

The following fields from the source description are used to display information in the lockscreen:

FieldPurpose
source.metadata.titleLockscreen title
source.metadata.artist with fallback to source.metadata.subtitleLockscreen artist
source.metadata.displayIconUri with fallback to source.posterLockscreen image

The following fields allow additional configuration for the lockscreen behaviour on iOS:

FieldPurpose
source.metadata.nowPlayingServiceIdentifierInfo property service identifier
source.metadata.nowPlayingContentIdentifierInfo property content identifier
notification_androidnotification_iosnotification_web
AndroidiOSWeb

Configuration

The mediaControl property of PlayerConfiguration hosts properties that affect the notifications, media sessions and controls on each platform. The following table describes the available configuration options:

FieldPlatformPurposeDefault
mediaSessionEnabledWeb & AndroidIf disabled, no media session properties or actions will be set.true
skipForwardIntervalWeb, iOS & AndroidIf disabled, no media session properties or actions will be set.5 (Web & Android), 15 (iOS)
skipBackwardIntervalWeb, iOS & AndroidIf disabled, no media session properties or actions will be set.5 (Web & Android), 15 (iOS)
convertSkipToSeekiOS & AndroidA flag that allows next/previous track commands to be interpreted as skip forward/backward commands, according to the configured skip intervals.false
allowLivePlayPauseiOS & AndroidA flag that indicates whether play/pause controls for live streams are enabled.true (iOS), false (Android)
seekToLiveOnResumeiOS & AndroidA flag that controls whether to seek to the live edge when resuming a paused live stream. Note that allowLivePlayPause needs to be enabled for this property to have effect.false