A sample demonstrating how to continue audio/video playback in the background with OptiView Player (formerly THEOplayer) using Android's MediaSession API and a foreground service.
The app uses the Media Session Connector to report the playback state and receive player requests
and show media notification controls, handle audio focus changes, and pause playback
when audio output changes (e.g. headphone removal). It uses Jetpack Compose with DefaultUI
for the player UI.
PlayerActivity uses Compose with
DefaultUI and binds to a MediaPlaybackService foreground service that keeps playback alive in the
background. AudioFocusManager handles audio focus transitions, and MediaNotificationBuilder creates
the media-style notification with playback controls.
More information on how to use the Media Session Connector can be found on its documentation page and the Android developer pages describing Building a video app and Building an audio app.
- Open this repository in Android Studio.
- Select the
background-playbackrun configuration. - Build and run on a device or emulator.
This project uses THEOplayer from the official Maven repository.
The repository is declared in the project-level settings.gradle.kts:
dependencyResolutionManagement {
repositories {
maven { url = uri("https://maven.theoplayer.com/releases") }
}
}Dependencies are managed through a version catalog and declared
in the module-level build.gradle.kts:
dependencies {
implementation(libs.theoplayer)
implementation(libs.theoplayer.ui)
implementation(libs.theoplayer.connector.mediasession)
implementation(libs.media)
}To play your own streams, add a THEOplayer license from the THEOportal in
common/src/main/res/values/values.xml:
<string name="theoplayer_license">YOUR_LICENSE_HERE</string>The license is picked up automatically via the <meta-data> tag in
AndroidManifest.xml.
Video sources are defined in SourceManager.
This project is licensed under the BSD 3 Clause License - see the LICENSE file for details.