🎬 Running Videos in Background with VdoCipher in Flutter🎶
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 version 2.7.0, vdocipher_flutter package offers smooth support for background playback, enabling uninterrupted media enjoyment across various device activities. 🌟
This background playback feature is currently only available on the Android platform.
⚙️ Configuring Background Playback
🎵 Playback Modes
The vdocipher_flutter package 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 Android (for SDK version 34 or higher), add the following permission to your AndroidManifest.xml
file, located at android/app/src/main/AndroidManifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application
..>
..
</application>
</manifest>
This permission is required for media playback to continue in the background.
2. Configure Background Playback
To set up background playback, configure the playback mode before your VdoPlayer
widget is added to the widget tree. You can configure the playback mode either in the widget’s initState
(as shown in the example) or directly in main.dart
if you choose to set it once and not change it after app initialization.
class VdoPlaybackViewState extends State<VdoPlaybackView> {
void initState() {
super.initState();
VdoPlayerSetting vdoPlayerSetting =
const VdoPlayerSetting(vdoPlaybackMode: VdoPlaybackMode.continuePlaybackOnBackPress);
vdoPlayerSetting.apply();
}
}
Replace continuePlaybackOnBackPress
in the snippet with the mode that best suits your app's needs:
- defaultMode: Stops playback when the player screen is closed.
- continuePlaybackOnBackPress: Continues playback when the app is minimized but stops when fully closed.
- continuePlaybackOnAppExit: Keeps playback running even after the app is closed.
If playback behavior is not set, the player will default to defaultMode behavior.