DEV Community

HarmonyOS
HarmonyOS

Posted on

How to play the next song with AVPlayer?

Read the original article:How to play the next song with AVPlayer?

Context

Using AVPlayer allows for end-to-end playback of raw media resources.

Description

After a song finishes playing, it automatically switches to the next song. However, if the next song is still buffering, manually clicking to play the next song does not work as expected.

Solution

To switch to the next song after the current one finishes playing, you need to reset the media resource of AVPlayer. Both the current state of AVPlayer and the media resource can cause playback issues.

1.Check if the current AVPlayer state is idle: The AVPlayer will only enter the idle state after it is created using createAVPlayer() or after the reset() method is called.

Based on the issue described, after the current music playback is completed, it is necessary to call the reset() interface to bring the AVPlayer into an idle state before setting a new media resource. The code is as follows:

switch (state) {
  case 'completed':
    this.state = AudioPlayerState.COMPLETED;
    this.avPlayer.reset();
    this.playNextAuto();
    break;
}
async
playNextAuto() {
  let avFileDescriptor: media.AVFileDescriptor = { fd: url.fd, offset: url.offset, length: url.length };
  this.avPlayer.fdSrc = avFileDescriptor;
  Logger.info(TAG, 'avPlayer.url:' + this.avPlayer.fdSrc);
}
Enter fullscreen mode Exit fullscreen mode

2.Confirm that the resource path and format of the media resource being played are valid, as follows:

  • If using local resources for playback, ensure that the resource file is available and access the corresponding resource using the application sandbox path, referring to the method for obtaining the application file path.
  • If using a network playback path, declare the permission: ohos.permission.INTERNET;
  • If using ResourceManager.getRawFd to open the HAP resource file descriptor, refer to the ResourceManager API reference for usage methods.
  • Ensure that the supported playback formats and protocols are used.

3.For network playback, it is necessary to monitor the network playback buffer information to report the buffer percentage and playback progress. The code is as follows:

 avPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => {
   console.info('bufferingUpdate called,and infoType value is:' + infoType + ', value is :' + value)
 })
Enter fullscreen mode Exit fullscreen mode

Following the above approach, the cause of the current playback issue was identified as: after the music playback was completed, the reset() method was not called to restore the player to an idle state before switching the audio resource URI. By calling the player's reset() method to switch the URI, playback returned to normal, and the issue was resolved.

Key Takeaways

When switching to the next song after the current music playback is completed, it is necessary to set the media resource for the next song and ensure the validity of the resource. Media resources can only be set when the player is in an idle state, so it is essential to restore the player to an idle state.

Written by Mehmet Karaaslan

Top comments (0)