Class MediaPlayer
Inheritance
Namespace: Glitch9.NativeMediaPlayer
Assembly: .dll
Syntax
public static class MediaPlayer
Properties
| Edit this page View SourceBackgroundControlsEnabled
Show or hide the native background media controls (Android: MediaStyleNotification / iOS: MPNowPlayingInfoCenter). It's sometimes unstable on runtime, so it's recommended to set this property before playing media.
Declaration
public static bool BackgroundControlsEnabled { get; set; }
Property Value
Type | Description |
---|---|
bool |
BackgroundPlayEnabled
Indicates if the background mode is enabled.
Note that this property DOES NOT have platform-specific implementation and is handled by Glitch9.NativeMediaPlayer scene object within Unity.
Declaration
public static bool BackgroundPlayEnabled { get; set; }
Property Value
Type | Description |
---|---|
bool |
Remarks
True by default.
CurrentMediaItem
Gets the current MediaItem.
Declaration
public static MediaItem CurrentMediaItem { get; }
Property Value
Type | Description |
---|---|
MediaItem |
CurrentMediaItemIndex
Retrieves the current MediaItem's index from the native side of the plugin.
Declaration
public static int CurrentMediaItemIndex { }
Property Value
Type | Description |
---|---|
int |
CurrentMediaItemUriType
Declaration
public static UriType CurrentMediaItemUriType { get; }
Property Value
Type | Description |
---|---|
UriType |
CurrentPlaylist
Gets the current playlist.
Declaration
public static Playlist CurrentPlaylist { get; }
Property Value
Type | Description |
---|---|
Playlist |
CurrentPlaylistIndex
Gets the current Playlist index.
Declaration
public static int CurrentPlaylistIndex { get; }
Property Value
Type | Description |
---|---|
int |
HasNextMediaItem
Checks if there is a next MediaItem.
Declaration
public static bool HasNextMediaItem { get; }
Property Value
Type | Description |
---|---|
bool | True if there is a next MediaItem; otherwise, false. |
HasNextPlaylist
Checks if there is a next Playlist available.
Declaration
public static bool HasNextPlaylist { get; }
Property Value
Type | Description |
---|---|
bool |
HasPreviousMediaItem
Checks if there is a previous MediaItem.
Declaration
public static bool HasPreviousMediaItem { get; }
Property Value
Type | Description |
---|---|
bool | True if there is a previous MediaItem; otherwise, false. |
HasPreviousPlaylist
Checks if there is a previous Playlist available.
Declaration
public static bool HasPreviousPlaylist { get; }
Property Value
Type | Description |
---|---|
bool |
IsInitialized
Indicates if the NativeMediaPlayer plugin is initialized.
Declaration
public static bool IsInitialized { get; }
Property Value
Type | Description |
---|---|
bool |
IsLoading
Indicates if the NativeMediaPlayer plugin is currently loading media from remote URL (http/https).
Declaration
public static bool IsLoading { }
Property Value
Type | Description |
---|---|
bool |
IsPlaying
Indicates if the NativeMediaPlayer plugin is currently playing media.
Declaration
public static bool IsPlaying { }
Property Value
Type | Description |
---|---|
bool |
IsReady
Checks if the NativeMediaPlayer plugin is ready to play media.
Declaration
public static bool IsReady { get; }
Property Value
Type | Description |
---|---|
bool |
LastDuration
Gets the duration of the last played MediaItem.
Declaration
public static float LastDuration { get; }
Property Value
Type | Description |
---|---|
float |
LastError
Gets the last error message from the NativeMediaPlayer plugin.
Declaration
public static string LastError { get; }
Property Value
Type | Description |
---|---|
string |
LastMediaItemIndex
Gets the index of the last MediaItem played.
Declaration
public static int LastMediaItemIndex { get; }
Property Value
Type | Description |
---|---|
int |
LastPosition
Gets the last playback position of the last played MediaItem.
Declaration
public static float LastPosition { get; }
Property Value
Type | Description |
---|---|
float |
ListenerId
Name of the listener object in the scene. The listener object in the latest version is Glitch9.NativeMediaPlayer. In previous versions, it was MediaEventListener.
Declaration
public static string ListenerId { get; set; }
Property Value
Type | Description |
---|---|
string |
Logger
Gets or sets the logger instance.
Declaration
public static ILogger Logger { get; set; }
Property Value
Type | Description |
---|---|
ILogger |
PlaybackState
Declaration
public static PlaybackState PlaybackState { get; }
Property Value
Type | Description |
---|---|
PlaybackState |
Playlists
Gets the collection of playlists.
Declaration
public static IReadOnlyDictionary<int, Playlist> Playlists { get; }
Property Value
Type | Description |
---|---|
IReadOnlyDictionary<int, Playlist> |
PreBufferDuration
Controls how many seconds of the MediaItem to pre-buffer before playing when it is StreamingUrl.
Declaration
public static int PreBufferDuration { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
This property is shared between Unity and native plugins via PlayerPrefs.
RepeatMode
Repeat mode control property, with platform-specific implementation.
0: No repeat, 1: Repeat all, 2: Repeat one
Declaration
public static RepeatMode RepeatMode { get; set; }
Property Value
Type | Description |
---|---|
RepeatMode |
SeekIncrement
Controls how many seconds to seek forward or backward.
Declaration
public static int SeekIncrement { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
This property is shared between Unity and native plugins via PlayerPrefs.
ShuffleModeEnabled
Shuffle mode control property, with platform-specific implementation.
True: Shuffle mode enabled, False: Shuffle mode disabled
Declaration
public static bool ShuffleModeEnabled { }
Property Value
Type | Description |
---|---|
bool |
Volume
Volume control property, with platform-specific implementation.
Value range: 0.0 to 1.0
Declaration
public static float Volume { }
Property Value
Type | Description |
---|---|
float |
onDownloadProgress
Called periodically to report download progress of the current media item.
Declaration
public static UnityEvent<string, float> onDownloadProgress { get; }
Property Value
Type | Description |
---|---|
UnityEvent<string, float> |
onInitialized
Called when the media player is initialized and ready.
Declaration
public static UnityEvent onInitialized { get; }
Property Value
Type | Description |
---|---|
UnityEvent |
onIsLoadingChanged
Called when the player starts or stops loading media.
Declaration
public static UnityEvent<bool> onIsLoadingChanged { get; }
Property Value
Type | Description |
---|---|
UnityEvent<bool> |
onIsPlayingChanged
Called when the playback playing state changes (playing or paused).
Declaration
public static UnityEvent<bool> onIsPlayingChanged { get; }
Property Value
Type | Description |
---|---|
UnityEvent<bool> |
onMediaItemTransition
Called when the media item is changed during playback.
Declaration
public static UnityEvent<string> onMediaItemTransition { get; }
Property Value
Type | Description |
---|---|
UnityEvent<string> |
onPlaybackStateChanged
Called when the playback state changes (e.g., Playing, Paused, Stopped).
Declaration
public static UnityEvent<PlaybackState> onPlaybackStateChanged { get; }
Property Value
Type | Description |
---|---|
UnityEvent<PlaybackState> |
onPlayerError
Called when a playback error occurs.
Declaration
public static UnityEvent<string> onPlayerError { get; }
Property Value
Type | Description |
---|---|
UnityEvent<string> |
onPlaylistLoaded
Called when a playlist is successfully loaded.
Declaration
public static UnityEvent onPlaylistLoaded { get; }
Property Value
Type | Description |
---|---|
UnityEvent |
onRepeatModeChanged
Called when the repeat mode is changed.
Declaration
public static UnityEvent<RepeatMode> onRepeatModeChanged { get; }
Property Value
Type | Description |
---|---|
UnityEvent<RepeatMode> |
onShuffleModeEnabledChanged
Called when the shuffle mode is enabled or disabled.
Declaration
public static UnityEvent<bool> onShuffleModeEnabledChanged { get; }
Property Value
Type | Description |
---|---|
UnityEvent<bool> |
Methods
| Edit this page View SourceAddMediaItem(MediaItem, int)
Declaration
public static void AddMediaItem(MediaItem item, int mediaItemIndex = -1)
Parameters
Type | Name | Description |
---|---|---|
MediaItem | item | The MediaItem to add. |
int | mediaItemIndex | The index at which to add the MediaItem. |
Remarks
Note that altering the Playlist on runtime can cause unexpected behavior. It is safer to create a new Playlist instead.
AddPlaylist(Playlist, Action)
Add a new Playlist to the list of playlists.
Declaration
public static void AddPlaylist(Playlist playlist, Action onPlaylistLoaded)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to add. |
Action | onPlaylistLoaded | The action to perform when the playlist is loaded. |
AddPlaylist(Playlist, bool, int, float)
Add a new Playlist to the list of playlists.
Declaration
public static void AddPlaylist(Playlist playlist, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to add. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
AddPlaylist(Playlist, int, float)
Add a new Playlist to the list of playlists.
Declaration
public static void AddPlaylist(Playlist playlist, int startMediaItemIndex, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to add. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
ClearPlaylist()
Declaration
public static void ClearPlaylist()
Remarks
Note that altering the Playlist on runtime can cause unexpected behavior. It is safer to create a new Playlist instead.
FadeIn(float)
Fades in the current MediaItem over a specified duration. (in seconds)
Declaration
public static void FadeIn(float duration)
Parameters
Type | Name | Description |
---|---|---|
float | duration | The duration of the fade-in effect. |
FadeOut(float)
Fades out the current MediaItem over a specified duration. (in seconds)
Declaration
public static void FadeOut(float duration)
Parameters
Type | Name | Description |
---|---|---|
float | duration | The duration of the fade-out effect. |
FastForward()
Fast-forwards the current MediaItem by SeekIncrement.
Declaration
public static void FastForward()
GetCurrentPosition()
Gets the current playback position of the MediaItem.
Declaration
public static float GetCurrentPosition()
Returns
Type | Description |
---|---|
float | The current playback position. |
GetDuration()
Gets the duration of the current MediaItem.
Declaration
public static float GetDuration()
Returns
Type | Description |
---|---|
float | The duration of the current MediaItem. |
GetMediaItem(int, int)
Gets a media item by its index and optional playlist index.
Declaration
public static MediaItem GetMediaItem(int mediaItemIndex, int playlistIndex = -1)
Parameters
Type | Name | Description |
---|---|---|
int | mediaItemIndex | The index of the media item to get. |
int | playlistIndex | The index of the playlist to get the media item from. If -1, uses the current playlist index. |
Returns
Type | Description |
---|---|
MediaItem | The media item if found; otherwise, null. |
GetNativePlayerName()
Declaration
public static string GetNativePlayerName()
Returns
Type | Description |
---|---|
string |
GetPlaylist(int)
Gets a playlist by its index.
Declaration
public static Playlist GetPlaylist(int playlistIndex)
Parameters
Type | Name | Description |
---|---|---|
int | playlistIndex | The index of the playlist to get. |
Returns
Type | Description |
---|---|
Playlist | The playlist if found; otherwise, null. |
GetShuffleOrder()
Gets the shuffle order of the current Playlist.
Declaration
public static int[] GetShuffleOrder()
Returns
Type | Description |
---|---|
int[] | An array representing the shuffle order. |
Initialize(MediaItem, bool, float)
Initialize the Glitch9.NativeMediaPlayer plugin with a MediaItem.
Declaration
public static void Initialize(MediaItem mediaItem, bool playWhenReady = false, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
MediaItem | mediaItem | The MediaItem to initialize the plugin with. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
float | startPosition | The position to start playback from. |
Remarks
Use this only when NativeMediaPlayerSettings.AutoInit is set to false, and you are initializing the NativeMediaPlayer plugin manually. You also have to initialize AndroidSettings and iOSSettings manually.
Initialize(Playlist, bool, int, float)
Initialize the Glitch9.NativeMediaPlayer plugin with a Playlist.
Declaration
public static void Initialize(Playlist playlist, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to initialize the plugin with. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
Remarks
Use this only when NativeMediaPlayerSettings.AutoInit is set to false, and you are initializing the NativeMediaPlayer plugin manually. You also have to initialize AndroidSettings and iOSSettings manually.
Initialize(IEnumerable<Playlist>, bool, int, int, float)
Initialize the Glitch9.NativeMediaPlayer plugin with multiple Playlists.
Declaration
public static void Initialize(IEnumerable<Playlist> playlists, bool playWhenReady = false, int startMediaItemIndex = 0, int startPlaylistIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Playlist> | playlists | The list of Playlists to initialize the plugin with. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
int | startPlaylistIndex | The index of the Playlist to start playback from. |
float | startPosition | The position to start playback from. |
Remarks
Use this only when NativeMediaPlayerSettings.AutoInit is set to false, and you are initializing the NativeMediaPlayer plugin manually. You also have to initialize AndroidSettings and iOSSettings manually.
Next()
Skips to the next MediaItem.
Declaration
public static void Next()
OnApplicationPause(bool)
Declaration
public static void OnApplicationPause(bool pause)
Parameters
Type | Name | Description |
---|---|---|
bool | pause |
OnApplicationQuit()
Declaration
public static void OnApplicationQuit()
Pause()
Pauses media playback of the current MediaItem.
Declaration
public static void Pause()
Play()
Play the current MediaItem. Please make sure that everything is set up correctly before calling this method.
Declaration
public static void Play()
Play(MediaItem)
Plays a MediaItem from the current Playlist. If the MediaItem is not in the Playlist, it will be added.
Declaration
public static void Play(MediaItem mediaItem)
Parameters
Type | Name | Description |
---|---|---|
MediaItem | mediaItem | The MediaItem to play. |
Play(int, float)
Play the current MediaItem at the specified index and the position. Please make sure that everything is set up correctly before calling this method.
Declaration
public static void Play(int mediaItemIndex, float startingPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
int | mediaItemIndex | The index of the MediaItem to play. |
float | startingPosition | The position to start playback from. |
PlayUrl(string)
Plays a MediaItem from the current Playlist using a remote URL. If the MediaItem is not in the Playlist, it will be added.
Declaration
public static void PlayUrl(string fileUrl)
Parameters
Type | Name | Description |
---|---|---|
string | fileUrl | The remote URL of the MediaItem to play. |
Previous()
Skips to the previous MediaItem.
Declaration
public static void Previous()
Release()
[CAUTION] Use this method very carefully. It will release all native audio player instances, and it won't be recoverable. You have to reinitialize the player to use it again.
Declaration
public static void Release()
RemoveMediaItem(int)
Declaration
public static void RemoveMediaItem(int mediaItemIndex)
Parameters
Type | Name | Description |
---|---|---|
int | mediaItemIndex | The index of the MediaItem to remove. |
Remarks
Note that altering the Playlist on runtime can cause unexpected behavior. It is safer to create a new Playlist instead.
RemovePlaylist(int)
Remove a Playlist from the list of playlists. Note: Removing the current playlist will stop playback.
Declaration
public static void RemovePlaylist(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | The index of the Playlist to remove. |
Rewind()
Rewinds the current MediaItem by SeekIncrement.
Declaration
public static void Rewind()
SeekTo(float)
Seeks to a specific time in the current MediaItem.
Declaration
public static void SeekTo(float time)
Parameters
Type | Name | Description |
---|---|---|
float | time | The time to seek to. |
SelectPlaylist(int)
Change the current Playlist to the one at the specified index.
Declaration
public static void SelectPlaylist(int playlistIndex)
Parameters
Type | Name | Description |
---|---|---|
int | playlistIndex | The index of the Playlist to change to. |
SelectPlaylist(int, bool, int, float)
Change the current Playlist to the one at the specified index.
Declaration
public static void SelectPlaylist(int playlistIndex, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
int | playlistIndex | The index of the Playlist to change to. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
SelectPlaylist(int, int, float)
Change the current Playlist to the one at the specified index.
Declaration
public static void SelectPlaylist(int playlistIndex, int startMediaItemIndex, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
int | playlistIndex | The index of the Playlist to change to. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
SetMediaItem(MediaItem, bool, int, float)
Declaration
public static void SetMediaItem(MediaItem mediaItem, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
MediaItem | mediaItem | The MediaItem to set. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
SetMediaItem(MediaItem, int, bool, int, float)
Replaces the current playlist with the new playlist.
Declaration
public static void SetMediaItem(MediaItem mediaItem, int playlistIndex, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
MediaItem | mediaItem | The MediaItem to set. |
int | playlistIndex | |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
SetPlaybackSpeed(float)
Alters the playback speed of the native player. It will be automatically reset to 1.0 when a new media item is played.
Declaration
public static void SetPlaybackSpeed(float speed)
Parameters
Type | Name | Description |
---|---|---|
float | speed | The playback speed to set. |
SetPlaylist(Playlist, bool, int, float)
Replaces the current playlist with the new playlist.
Declaration
public static void SetPlaylist(Playlist playlist, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to set. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
SetPlaylist(Playlist, int, bool, int, float)
Replaces the playlist at the specified index with the new playlist.
Declaration
public static void SetPlaylist(Playlist playlist, int playlistIndex, bool playWhenReady = false, int startMediaItemIndex = 0, float startPosition = 0)
Parameters
Type | Name | Description |
---|---|---|
Playlist | playlist | The Playlist to Set. |
int | playlistIndex | The index of the Playlist to replace. |
bool | playWhenReady | Whether to start playback when the plugin is initialized. |
int | startMediaItemIndex | The index of the MediaItem to start playback from. |
float | startPosition | The position to start playback from. |
Stop()
Stops media playback of the current MediaItem.
Declaration
public static void Stop()