How to solve the problem that the progress status of the audio playback control notification bar does not update
Problem Description
When playing audio, the following situations occur when creating and setting the AVSessionManager session:
- The progress in the notification bar will not be updated after setting the playback status;
- After pulling down the notification bar, the update returns to normal;
- After locking the screen, open it to resume normal updates.
Background Knowledge
AVSession Kit : System-provided audio and video control services that centrally manage all audio and video activities within the system.
Background Tasks Kit : Background task management. After an app transitions to the background, resources are strictly controlled. Apps should select appropriate background tasks to ensure continued operation.
Media Session Provider : Audio and video applications control media-related information from the media session controller and respond to playback control commands issued by the media session controller.
Troubleshooting Process
- For media playback, the progress bar in the notification bar is not effective. First, check whether the long-running task of AUDIO_PLAYBACK has been applied.
- Check whether the resource duration, playback status, playback position, speed, and other information are set correctly. The broadcast control center will use this information to display the progress.
Analysis Conclusion
An error in the initialization of the playback status in the broadcast control center caused the playback progress calculation to fail and not update. The playback status returned to normal after pulling down the status bar or locking the screen and resetting it.
Solution
1.Apply for a long-duration task to avoid entering the Suspend state and causing playback to stop. When your app needs to implement features such as background playback, you need to use the BackgroundTasks Kit to apply for the corresponding long-duration task to avoid entering the Suspend state. For media playback, you need to apply for the AUDIO_PLAYBACK long-duration task. The sample code is as follows:
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}`);
});Copy codeCopy code
2.Correctly initialize the playback resources of the playback control center and update them when the status changes to prevent playback from stopping or playback progress from being miscalculated. The system's playback control center will automatically calculate the playback progress based on the resource duration, playback status (pause, play), playback position, speed, and other information set by the application, without the need for the application to update the playback progress in real time. The sample code is as follows:
let playbackState: AVSessionManager.AVPlaybackState = {
state: AVSessionManager.PlaybackState.PLAYBACK_STATE_PLAY,
position: {
elapsedTime: 1000,
updateTime: new Date().getTime(),
},
speed: 1.0,
bufferedTime: 14000,
};
session.setAVPlaybackState(playbackState, (err) => {
if (err) {
console.error(`Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`SetAVPlaybackState successfully`);
}
});
Notice
- The session playback state (AVPlaybackState) needs to be updated only when the playback state (state), playback position (position), and playback speed (speed) of the playback media change, otherwise the system will cause calculation errors.
- When the actual playback starts, the application reports the progress starting playback position (position); if the playback is in a buffering state, you can first report the playback state as AVSessionManager.PlaybackState.PLAYBACK_STATE_BUFFERING to notify the system not to refresh the progress.
Verification Result
The system's playback control center automatically calculates and refreshes the playback progress based on the playback status. The application only needs to update when the playback rate, position, and other information change.
Top comments (0)