Creating Playlist at Runtime
Creating a new playlist at runtime with the MediaPlayer
class is straightforward. This guide will walk you through the process step-by-step.
1. Determine the Source of Your Media Files
Before creating a playlist, you need to decide where your media files are located. The UriType
enum in the MediaPlayer
class helps you specify the source of your media files. Here are the available options:
UriType
Options
UriType
OptionsStreamingAssets: The media file is located in the StreamingAssets folder.
Android uses 'MediaPlayer' to play the media file.
iOS uses 'AVAudioPlayer' to play the media file.
RemoteURL: The media file is located at a remote URL (e.g., http://www.example.com/media.mp3).
DownloadAndPlay: The media file will be downloaded to the 'PersistentDataPath' before playing.
Android uses 'MediaPlayer' to play the media file.
iOS uses 'AVAudioPlayer' to play the media file.
PersistentDataPath: The media file is located in the 'PersistentDataPath'.
Android uses 'MediaPlayer' to play the media file.
iOS uses 'AVAudioPlayer' to play the media file.
2. Creating Playlists at Runtime
Step 1: Define Your Media Items
First, you need to create MediaItem
objects that will be part of your playlist. Each MediaItem
represents a piece of media that you want to add to your playlist.
Example:
Step 2: Create the Playlist
Once you have your MediaItem
objects, you can create a Playlist
object and add these items to it.
Example:
Step 3: Setting the Playlist to the Media Player
If you want to add a new playlist to your existing list of playlists, you can use the AddPlaylist
method.
Example:
Or you can replace the current playlist with SetPlaylist
method.
Example:
Full Example
Here's a complete example of how to create a new playlist and add it to the MediaPlayer
:
Last updated