Read the original article:Notification Bar Progress Status Not Updated for Audio Playback Control
Notification Bar Progress Status Not Updated for Audio Playback Control
Problem Description
When playing audio, the notification bar progress does not update after creating and setting the AVSessionManager session.
The progress bar fails to update after setting the playback state.
-
Progress updates recover normally after:
- Pulling down the notification bar.
- Unlocking the screen after locking.
Background Knowledge
- AVSession Kit: System-provided audio/video control service to manage all media behaviors.
- Background Tasks Kit: Manages background tasks; apps must request long-running tasks to continue operations when backgrounded.
- AVSession Provider: Audio/video apps control session information and respond to playback commands from the media session controller.
Troubleshooting Process
-
Check long-running task:
- Verify if
AUDIO_PLAYBACKlong-running task is requested to prevent app suspension.
- Verify if
-
Validate playback state parameters:
- Ensure duration, playback state, position, and speed are correctly set for the media control center to calculate progress.
Analysis Conclusion
The media control center's playback state initialization failed, causing incorrect progress calculation. Progress updates resumed after:
- Redrawing the notification bar.
- Resuming playback state after unlocking the screen.
Solution
- Request long-running task to avoid suspension:
backgroundTaskManager.startBackgroundRunning(this.context,
backgroundTaskManager.BackgroundMode.AUDIO_PLAYBACK, wantAgentObj)
.then(() => {
console.info("Operation startBackgroundRunning succeeded");
}).catch((error: BusinessError) => {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
});
- Initialize and update playback state correctly:
let playbackState: AVSessionManager.AVPlaybackState = {
state: AVSessionManager.PlaybackState.PLAYBACK_STATE_PLAY, // Playback state
position: {
elapsedTime: 1000, // Elapsed time in ms
updateTime: new Date().getTime(), // Timestamp in ms
},
speed: 1.0, // Playback speed (default: 1.0)
bufferedTime: 14000, // Buffered time in ms
};
session.setAVPlaybackState(playbackState, (err) => {
if (err) {
console.error(`Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`SetAVPlaybackState successfully`);
}
});
Verification Result
- Notification bar progress updates return to normal after applying the long-running task and correct playback state initialization.
- Verified on HarmonyOS devices with media playback scenarios.
Related Documents or Links
https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-avsession
Top comments (0)