Read the original article:How do I play music in the background?
Context
An audio and video application needs to access the AVSession service as a provider in order to display media information in the controller (for example, Media Controller) and respond to playback control commands delivered by the controller.
- AVMetadata: media data related attributes, including the IDs of the current media asset (assetId), previous media asset (previousAssetId), and next media asset (nextAssetId), title, author, album, writer, and duration.
- AVPlaybackState: playback state attributes, including the playback state, position, speed, buffered time, loop mode, media item being played (activeItemId), custom media data (extras), and whether the media asset is favorited (isFavorite).
Description
The AVSession service provides a unified way for applications to manage and control media playback across the system. By creating an AVSession, an audio or video application can expose its playback information (such as media title, author, album, duration, and playback state) to the system and external controllers (for example, the Media Controller). This allows users to see the current playback status and control playback through system UI elements, wearable devices, or connected accessories.
Without AVSession, when a third-party application is switched to the background or when the device screen is locked, playback is forcibly paused by the system and the application remains unaware of this interruption. This leads to a poor user experience, since users may expect media to continue in the background.
By leveraging AVSession together with the BackgroundTaskManager, applications can request a continuous background task, ensuring that playback continues smoothly even during background execution or screen lock events. At the same time, AVSession ensures consistent control: the Media Controller can send commands (play, pause, next, previous, etc.) to the application, and the app can update playback state and metadata accordingly. This approach provides a system-level, standardized, and user-friendly solution for handling media playback in HarmonyOS.
Solution / Approach
To enable the application to continue the playback in the background, request a continuous task and access the AVSession capability, which allows the control panel of the Media Controller to control the playback behavior of third-party applications.
Key Takeaways
- AVSession enables applications to share media information and respond to playback controls from the Media Controller.
- To support background playback (including screen lock scenarios), the app must request a continuous task.
- AVMetadata holds media-related attributes such as title, author, album, duration, and asset IDs.
- AVPlaybackState manages playback details like position, speed, loop mode, favorites, and custom extras.
- Using BackgroundTaskManager ensures uninterrupted playback for third-party applications.
- Integration with the Media Controller’s control panel is essential for a seamless user experience.
Additional Resources
https://developer.huawei.com/consumer/en/doc/harmonyos-guides/using-avsession-developer
Top comments (0)