🎬 Background Playback with VdoCipher SDK 🎶
Background playback allows media to continue playing seamlessly while multitasking or switching apps. This feature ensures that music, podcasts, or videos remain uninterrupted, even when the screen is locked or the app is exited. Starting with SDK version 1.27.0, the VdoCipher Android SDK offers smooth support for background playback, enabling uninterrupted media enjoyment across various device activities. 🌟
⚙️ Configuring Background Playback
🎵 Playback Modes
The VdoCipher SDK offers three playback modes, each providing a different level of persistence:
- Default
- Continue playback on back press
- Continue playback on app exit
The media will immediately pause when the user closes or navigates away from the player screen.
Ideal for applications like e-learning platforms, where it's important for users to stay focused on the content without switching between apps.📚
Playback pauses when the app is completely exited, but continues if the app is minimized or the back button is pressed.
Ideal for apps where users might briefly switch tasks, like news or streaming apps. 📰
The media continues playing in the background, regardless of whether the app is minimized or fully exited.
Perfect for music and podcast apps where continuous playback is a core feature, offering an uninterrupted experience. 🎵
🛠️ How to Implement?
1. Add Permission for Media Playback Service
To enable background playback in your app, you need to declare the android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK
permission in your AndroidManifest.xml
file when target SDK version is set as 34 and higher.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application
..>
..
</application>
</manifest>
2. Configure Background Playback
To configure background playback, use the following code in your application class:
- Java
- Kotlin
public class MyApplication extends Application {
@Override public void onCreate() {
super.onCreate();
new VdoPlayerSetting.Builder()
// Choose the mode that fits your app
.setPlaybackBehavior(VdoPlaybackMode.CONTINUE_PLAYBACK_ON_BACK_PRESS)
.build()
.applySettings(getApplicationContext());
}
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
VdoPlayerSetting.Builder()
// Choose the mode that fits your app
.setPlaybackBehavior(VdoPlaybackMode.CONTINUE_PLAYBACK_ON_BACK_PRESS)
.build()
.applySettings(applicationContext)
}
}
Replace CONTINUE_PLAYBACK_ON_BACK_PRESS
in the snippet with the mode that best suits your app's needs:
- DEFAULT: Stops playback when the player screen is closed.
- CONTINUE_PLAYBACK_ON_BACK_PRESS: Continues playback when the app is minimized but stops when fully closed.
- CONTINUE_PLAYBACK_ON_APP_EXIT: Keeps playback running even after the app is closed.
If playback behavior is not set, the player will default to DEFAULT behavior.