Native Media Player
Glitch9 Inc.Main Docs.
  • Introduction
    • Native Media Player
    • Preparation
    • Initialization
  • Functionalities
    • Media Item
    • Media Metadata
    • Playlist
    • Media Events
    • UI Components
    • Android Settings
    • iOS Settings
  • Advanced
    • Manual Initialization
    • Creating Playlist at Runtime
    • Single Media Item Playlists
  • API Reference
    • MediaPlayer
      • Properties
      • Methods
    • MediaEvents
    • MediaItem
    • MediaMetadata
    • Playlist
    • Configuration Details
  • Support
    • FAQ
    • Troubleshooting
      • SeekTo (Seekbar) doesn't work
      • java.lang.NoClassDefFoundError
      • One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified
      • AndroidX Build Error: resource android:attr/lStar not found.
    • Update Logs
Powered by GitBook
On this page
  • Methods for Setting a Single Media Item
  • Full Example
  1. Advanced

Single Media Item Playlists

In some special scenarios, you might only have one media item to play. Using the SetMediaItem method can simplify the process of managing your media playback. This method allows you to set a single MediaItem directly, without the need to create a playlist manually.

Using SetMediaItem is beneficial when you:

  • Only need to play one media item.

  • Want to quickly replace the current media item without managing a full playlist.

  • Simplify your code for single media item scenarios.

Methods for Setting a Single Media Item

Method 1: Replacing the Current Playlist with a New Media Item

This method allows you to replace the entire playlist with a new media item. It is useful when you want to ensure that only one media item is being played.

Method Signature

public static void SetMediaItem(MediaItem mediaItem, int playlistIndex, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0f, Action onMediaItemLoaded = null)

Parameters:

  • mediaItem: The MediaItem to set.

  • playlistIndex: The index of the Playlist to set the MediaItem to.

  • playWhenReady: Whether to start playback when the plugin is initialized.

  • startMediaItemIndex: The index of the MediaItem to start playback from.

  • startPosition: The position to start playback from.

  • onMediaItemLoaded: The action to perform when the media item is loaded.

Example Usage:

MediaItem mediaItem = new MediaItem(UriType.StreamingAssets, "path/to/song.mp3");
MediaPlayer.SetMediaItem(mediaItem, 0, true, 0, 0f, OnMediaItemLoaded);

Method 2: Setting the Current Media Item Directly

This method allows you to set the current media item directly, using the current playlist index. This is a convenient shortcut if you don't need to specify a different playlist index.

Method Signature

csharpCopy codepublic static void SetMediaItem(MediaItem mediaItem, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0f, Action onMediaItemLoaded = null)

Parameters:

  • mediaItem: The MediaItem to set.

  • playWhenReady: Whether to start playback when the plugin is initialized.

  • startMediaItemIndex: The index of the MediaItem to start playback from.

  • startPosition: The position to start playback from.

  • onMediaItemLoaded: The action to perform when the media item is loaded.

Example Usage:

MediaItem mediaItem = new MediaItem(UriType.StreamingAssets, "path/to/song.mp3");
MediaPlayer.SetMediaItem(mediaItem, true, 0, 0f, OnMediaItemLoaded);

Full Example

Here is a complete example demonstrating how to create and play a single media item using both methods:

csharpCopy code// Define your media item
MediaItem mediaItem = new MediaItem(UriType.StreamingAssets, "path/to/song.mp3");

// Method 1: Replacing the current playlist with the new media item
MediaPlayer.SetMediaItem(mediaItem, 0, true, 0, 0f, OnMediaItemLoaded);

// Method 2: Setting the current media item directly
MediaPlayer.SetMediaItem(mediaItem, true, 0, 0f, OnMediaItemLoaded);

// Callback method for when the media item is loaded
void OnMediaItemLoaded()
{
    Debug.Log("Media item has been loaded and is ready to play.");
}
PreviousCreating Playlist at RuntimeNextMediaPlayer

Last updated 10 months ago