seekToMediaItem

expect open fun seekToMediaItem(index: Int)

Selects a media item from the playlist by its index.

Parameters

index

The index of the media item to play.

actual open fun seekToMediaItem(index: Int)
actual open fun seekToMediaItem(index: Int)

Selects a media item from the playlist by its index.

Parameters

index

The index of the media item to play.

This function takes an integer index as input, which represents the position of the desired media item within the astroMediaItems playlist. It performs the following steps:

  1. Safety Check: The function first verifies if the provided index is within the valid range of the playlist. If the index is greater than the size of the playlist minus one (indicating an out-of-bounds index), the function exits without making any changes.

  2. Update Index: If the index is valid, the currentMediaItemIndex property is updated to point to the selected media item. This essentially sets the current position within the playlist.

  3. Generate Howl Properties: Similar to the nextMediaItem function, a helper function named createHowlProperties is used. This function takes the source URL of the selected media item (astroMediaItems[index].source) and creates a new HowlProperties object. It also copies the current playback settings (volume, html5, loop, etc.) from the nativeMediaPlayer object to ensure consistent playback behavior.

  4. Create New Howl Instance: A new Howl instance is created using the generated HowlProperties. This effectively replaces the existing player with the selected media item, starting playback from the beginning.

By following these steps, the seekToMediaItem function allows you to navigate the playlist and play any media item based on its index.