UI Implementations

NativeMediaPlayer offers easy UI implementations with pre-coded scripts with easy-to-use custom inspectors. Create an empty object in Unity hierarchy and add component to find one of the UI scripts outlined below.

 

  • Media Button makes it easy to create your own media button such as play, pause, stop, loop, shuffle and more
  • Volume Bar makes it easy to create your own volume bar
  • Seek Bar makes it easy to create your own seek bar
  • UI Settings manages shared properties that apply to all UI elements listed above

 

Media Button

 

Media Button is an All-In-One component that can create all buttons that NativeMediaPlayer currently supports like the image below.

 

Media Button can become one of the many buttons

Choose Button Type to determine which button it is, and put in the necessary components such as Image and Events. Button colors are defined with UI Settings, and check Dark Mode if you want to use the Dark Mode colors.

 

Volume Bar

 

To create a volume bar, create a Unity built-in slider and put that into Volume Bar. The slider will behave as volume bar of the NativeMediaPlayer. In case you need events, add the events to the slider.

 

 

Seek Bar

 

Seek Bar also needs a Unity built-in slider. Create a slider and put it into Seek Bar. The slider will behave as seek bar of the NativeMediaPlayer. Update Interval is how often seek bar will be updated, and the default value is 0.5f which means that it will be updated every 0.5 seconds. You can add texts that will display the current position and the duration of the current media item.

 

 

 

 

UI Settings

 

UI Settings manages all shared properties of NativeMediaPlayer UI elements. It only manages UI colors in the current version.

 

Playlist

Playlist is a basic media source type that manages multiple media items for NativeMediaPlayer. It can only have one UriType which determines which native player the plugin is going to use, and pass the media data to the corresponding player. To add a new MediaItem to Playlist, simply click on the 'Add a new media item' button in the inspector menu.

 

If you are using StreamingAssets, drag the file directly into the box under 'Put in a file from StreamingAssets folder below'. The inspector will automatically detect the filename and use it as MediaUri of the media item. If you are using RemoteUrl, the box becomes a textbox where you can enter the URL.

 

NativeMediaPlayer will try to retrieve the media metadata using native APIs by the default, but you can also choose to customize media metadata and prevent from triggering those native APIs. Choosing custom media metadata will expand the inspector menu and show editable property fields of each metadata.

 

Expanded with custom metadata fields

Modifying the playlist

 

It’s possible to dynamically modify a playlist by adding, and removing media items. This can be done both before and during playback by calling the corresponding playlist API methods:

// Adds a media item at position 1 in the playlist.
playlist.AddMediaItem(1, new MediaItem(uriType, mediaUri));

// Removes the first item from the playlist.

playlist.RemoveMediaItem(0);
 
Replacing and clearing the entire playlist are also supported:
// Replaces the playlist with a new one.
List<MediaItem> newItems = new List<MediaItem>();
UriType uriType = playlist.Type;
newItems.Add(new MediaItem(uriType, mediaUri1));
newItems.Add(new MediaItem(uriType, mediaUri2));
playlist.SetMediaItems(newItems);

// Clears the playlist.
playlist.ClearMediaItems();

 

Querying the media items

 

Media items contained in a playlist can be individually queried using playlist.MediaItem[id].

// Quering title of the first item from the playlist.
string title = playlist.MediaItem[0].Title;

 

Methods

 

using NativeMediaPlayerCenter;

Playlist Playlist(int id,  UriType path, List<MediaItem> mediaItems = null, string title = null, string artist = null) Build a new Playlist
void AddMediaItem(MediaItem item = null) Add an empty MediaItem
void AddMediaItem(int index, MediaItem item) Add a MediaItem
void RemoveMediaItem(int index) Remove a MediaItem
void Sync() Send the Playlist to the native side, so that it can be readible from the native plugin. Use this fuction before preparing the native player.

 

Parameters

int Id Index number of the Playlist.
UriType Path An enum value that indicates where your sources are located.

short UriType.StreamingAssets = 0
short UriType.RemoteURL = 1
string Title Title of the Playlist.
void Artist Artist of the Playlist.
List<MediaItem> MediaItems MediaItems in the Playlist.
int Count Total number of the MediaItems.

 

MediaEventListener

MediaEventListener receives native callbacks. A game object with MediaEventListener.cs must be in the scene for the plugin to work properly. The chart below explains how the callbacks work in order.

 

 

NativeMediaPlayer player events

 

Namespace

 

using NativeMediaPlayerCenter;

 

Listeners

callback OnInit Called when NativeMediaPlayer plugin is initiated
callback OnReady Called when a new Playlist and its metadatas are fully registered to the native side
callback OnPrepared Called when player is able to immediately play a MediaItem
callback OnComplete Called when player finishes playing a MediaItem
callback OnError Called when error occurs
callback OnIsPlayingChanged(True) Called when player starts/resumes playing
callback OnIsPlayingChanged(False) Called when player stops/pauses playing
callback OnIsLoadingChanged(True) Called when player starts loading a MediaItem
callback OnIsLoadingChanged(False) Called when player finishes loading a MediaItem
callback OnIsBufferingChanged(True) Called when player starts buffering a MediaItem during playback
callback OnIsBufferingChanged(False) Called when player finished buffering a MediaItem during playback

 

Methods

void AddOnInitListener(UnityAction action) Add an action to OnInit
void AddOnReadyListener(UnityAction action) Add an action to OnReady
void AddOnPreparedListener(UnityAction action) Add an action to OnPrepared
void AddOnCompleteListener(UnityAction action) Add an action to OnComplete
void AddOnIsPlayingChangedListener(bool isPlaying, UnityAction action) Add an action to OnIsPlayingChanged
void AddOnIsLoadingChangedListener(bool isLoading, UnityAction action) Add an action to OnIsLoadingChanged
void AddOnIsBufferingChangedListener(bool isBuffering, UnityAction action) Add an action to OnIsBufferingChanged

 

 

MediaItem

The playlist API is based on MediaItems, which can be conveniently built using methods outlined below.

 

Simple media items

 

A media item can be easily created by calling the following methods:

// Creates a new media item with a file located in the StreamingAssets folder
MediaItem mediaItem = new MediaItem(UriType.StreamingAssets, filename);


// Creates a new media item with a remote url

MediaItem
 mediaItem = new MediaItem(UriType.RemoteUri, url);

 

A media item with a pre-configured metadata can also be created:

MediaMetadata mediaMetadata = new MediaMetadata();
...configure MediaMetadata
MediaItem mediaItem = new MediaItem(uriType, mediaUri, mediaMetadata);

 

Methods

 

using NativeMediaPlayerCenter;

MediaItem MediaItem(string uri, MediaMetadata metadata) Create a MediaItem.
void Sync() Send the MediaItem to the native side, so that it can be readible from the native plugin. Use this fuction before preparing the native player.

 

Properties

int Id The index number of the MediaItem
UriType Path An enum value that indicates where your sources are located.

short UriType.StreamingAssets = 0
short UriType.RemoteURL = 1
string MediaUri Uri path of the MediaItem.
bool HasCustomMediaMetadata (read-only) Returns true if the MediaItem is set to use a customized MediaMetadata and does not retrieve from the media file
string Title Title of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_TITLE (Android)
MPMediaItemPropertyTitle (iOS)
string Artist Artist of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_ARTIST (Android),
MPMediaItemPropertyArtist (iOS)
string AlbumTitle Album title of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_ALBUM (Android),
MPMediaItemPropertyAlbumTitle (iOS)
string AlbumArtist Album artist of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_ALBUM_ARTIST (Android),
MPMediaItemPropertyAlbumArtist (iOS)
string Genre Genre of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_GENRE (Android),
MPMediaItemPropertyGenre (iOS)
string ReleaseDate Release date of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_DATE (Android),
MPMediaItemPropertyReleaseDate (iOS)
Sprite Artwork Artwork of the MediaItem.

💡 Refers to following native parameters
METADATA_KEY_ART (Android),
MPMediaItemPropertyArtwork (iOS)

 

Mono Media Item (MonoMediaItem.cs)

 

Mono media item is a custom inspector embeded version of public class MediaItem. It can be used when NativeMediaPlayer is initiated with only one media item.

NativeMediaPlayer

NativeMediaPlayer is a bridge class that calls native APIs. It can be initiated manually by using the NativeMediaPlayer API below, or automatically by using MNPSettings.

 

Namespace

 

using NativeMediaPlayerCenter;

 

Methods

void Init(Playlist playlist, string listenerName, bool playWhenReady) Initiate the plugin with a default playlist

listenerName is name of the GameObject which has MediaEventListener.cs, and playWhenReady is player starts playing upon the plugin initiation
void Prepare(Playlist playlist, string listenerName, bool playWhenReady) Prepare a new playlist
void Play() Starts or resumes playback
void Pause() Pauses playback
void Stop() Stops playback
void Previous() Seeks to the previous MediaItem
void Next() Seeks to the next MediaItem
void FastForward() Seeks forward in the current MediaItem by SeekIncrement (seconds)
void Rewind() Seeks backward in the current MediaItem by SeekIncrement (seconds)
void SeekTo(float time) Seeks to a position specified in seconds in the current MediaItem
float GetDuration() Get the total duration of your current media item
float GetCurrentPosition() Get the current position of your current media item
bool HasPreviousMediaItem() Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled
bool HasNextMediaItem() Returns whether a next media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled
string RetrieveAlbumTitle() Returns a natively retrieved AlbumTitle of the current MediaItem
string RetrieveAlbumArtist() Returns a natively retrieved AlbumArtist of the current MediaItem
string RetrieveTitle() Returns a natively retrieved Title of the current MediaItem
string RetrieveArtist() Returns a natively retrieved Artist of the current MediaItem
string RetrieveGenre() Returns a natively retrieved Genre of the current MediaItem
string RetrieveReleaseDate() Returns a natively retrieved ReleaseDate of the current MediaItem
string RetrieveArtwork() Returns a natively retrieved Artwork data of the current MediaItem in base64 string format

 

Variables

bool isInit (read-only) Returns true if the plugin has been initiated
bool AutoSave Set this true if you want the player to automatically save and load following variables

float NativeMediaPlayer.Volume
int NativeMediaPlayer.RepeatMode
bool NativeMediaPlayer.ShuffleModeEnabled
int NativeMediaPlayer.CurrentMediaItemIndex
bool isPlaying (read-only)  Returns true if the player is playing
bool isLoading (read-only)  Returns true if the player is loading a MediaItem
int RepeatMode Get or set the repeat mode

int RepeatMode.Disabled = 0
int RepeatMode.RepeatOne = 1  
int RepeatMode.RepeatAll = 2   
bool ShuffleModeEnabled Get or set the shuffle mode
float Volume Get or set the player volume
Playlist CurrentPlaylist (read-only)  Returns the current playlist
UriType CurrentUriType (read-only)  Returns the current UriType of the current playlist
int GetCurrentMediaItemIndex (read-only)  Returns the index number of the current MediaItem
int[] GetShuffleOrder (read-only)  Returns the current play order
string Error (read-only)  Returns the lastest error message
int SeekIncrement (read-only)  Returns the seek increment
int PreBufferDuration (read-only)  Returns the pre-buffer duration

 

NMPSettings

NMPSettings automates the initiation process of the NativeMediaPlayer, and also configure its default values.

Native Media Player for Android & iOS

how to start

1. If you are using StreamingAssets, create a new folder named ‘StreamingAssets’ under Assets. Put your media sources in this folder.
2. Import NativeMediaPlayer prefab to your scene.
3. Configure NMPSettings, MediaEventListener, AndroidSettings, iOSSettings attached to the prefab using the inspector.
4. Import MediaSource prefab to your scene.

Choose which media source you want the initiate the player with

5. Import necessary media source prefabs (Playlist or MediaItem) to your scene and attach them to the MediaSource. Configure media items using the inspector of the prefabs. 
6. When adding an album art image, click the texture and check ‘Read/Write Enabled’ under Advanced in the inspector.

Check Read/Write Enabled

7. Set the MediaSource to NMPSettings's Media Source under Core Components.

Put your MediaSource in the Core Components

 

C# Scripts with Custom Inspector

NMPSettings.cs

Automates general configurations and default settings for NativeMediaPlayer, which is a bridge class that controls the native APIs.

💡 Included in NativeMediaPlayer.prefab

 

See this page for the details

https://codeqo.tistory.com/category/Native%20Media%20Player/NativeMediaPlayer

Easy-to-use custom inspector. No code required.


MediaEventListener.cs

Listens to the playback events from the native side and invokes callback events.

💡 Included in NativeMediaPlayer.prefab

 

See this page for the details

https://codeqo.tistory.com/category/Native%20Media%20Player/MediaEventListener


AndroidSettings.cs

Manages Android native settings.

💡 Included in NativeMediaPlayer.prefab

 

See this page for the details

https://codeqo.tistory.com/category/Native%20Media%20Player/Android

Android MediaStyleNotification preview in the custom inspector


iOSSettings.cs

Manages iOS native settings.

💡 Included in NativeMediaPlayer.prefab

 

See this page for the details

https://codeqo.tistory.com/category/Native%20Media%20Player/iOS


Playlist.cs

The playlist is implemented by all NativeMediaPlayer implementations. It enables sequential playback of multiple media items.

💡 public static class NativeMediaController is an extender class for Playlist management. This simplifies the implementation of multiple playlists.

💡 Included in Playlist.prefab

 

See this page for the details

https://codeqo.tistory.com/category/Native%20Media%20Player/Playlist


MediaSource.cs

A required script for NMPSettings.cs as a default media source. It offers 3 different media types.

  1. A MediaItem
  2. A Playlist
  3. Multiple Playlists

💡 Included in MediaSource.prefab

Supports multiple playlists (version 1.9.3+)

Update Logs v.1.9.0 (May 21th, 2022)

※ Changes
■ Major refactorying works
- Complete refactorying of C# scripts (Unity)
- Complete refactorying of Java scripts (Android)
- Complete refactorying of Objective-C scripts (iOS)
■ Sample scene layout rework
■ Custom inspector design rework
- Added new layouts
- Added new icons
- Added Android 12 Media Style Notification preview to custom inspector (AndroidSettings.cs)
■ UI components are divided to individual scripts
- MediaButton.cs : Add this to your media buttons (play/pause/stop/fast-forward/rewind/next/previous/loop/shuffle)
- SeekBar.cs : Add this to your seek bar
- VolumeBar.cs : Add this to your volume bar
■ Native settings are divided to Android and iOS settings
■ Playlist/Media Metadata rework (MediaItems.cs)
■ Both native sides now send metadatas as strings to Unity when a new metadata is ready
■ Native side now can send volume information directly to Unity's volume bar (This updates the volume bar properly if there is a volume change on the native side)
■ Default media metadata settings are moved to MediaMetadataRetriever.cs
■ Android media metadata handling scripts reworked
■ Added extra runnable to track current position of the player (to reduce delay with Unity UI components)
■ Added OnInit registerer/callbacks
■ Added Date validator/converter to Android side
■ All shuffle modes now behaves like ExoPlayer
■ Previous() now seeks to 0 when the current position is more than 1

※ New Methods
■ bool HasNextMediaItem() - returns true if next track is available
■ bool HasPreviousMediaItem() - returns true if previous track is available
■ string RetrieveTitle() - returns the current title
■ string RetrieveArtist() - returns the current artist
■ string RetrieveAlbumTitle() - returns the current album title
■ string RetrieveAlbumArtist() - returns the current album artist
■ string RetrieveGenre() - returns the current genre
■ string RetrieveReleaseDate() - returns the current release date
■ string RetrieveArtwork() - returns the current artwork (in Base64 encoded data format)
■ Added prefix/suffix options for default metadatas
■ Added disable condition to Next and Previous buttons

※ New Variables
(NativeMediaPlayerManager.cs)
■ int PreBufferDuration - manage how much data (in seconds) is buffered before player starts playing
(iOSSettings.cs)
■ bool UseSeekBar - turn on/off the seek bar that shows on the MPRemoteCommandCenter (iOS background media controller).

※ Bugfixes v.1.9.0
■ AVPlayer now returns the correct isPlaying value
■ AutoSave now works properly 
- loads when player is initiated
- saves when loading a new media item
■ Default Media Metadata issues resolved
■ void GetCurrentMediaItemIndex(NativeMediaPlayer.cs) now returns the unshuffled index number

'Native Media Player > Update Logs' 카테고리의 다른 글

Update Logs v.1.8.0 (April 19th, 2022)  (0) 2022.06.01

C# Code Reference

Namespace

 

using TaskLockMode;

 

 

Methods

 

LockTask.StartLockTask() Start task lock mode
LockTask.StopLockTask() Stop task lock mode

 

Variables

 

bool LockTask.isLockTaskModeRunning Returns true if task lock mode is running

 


Get it now on Unity Asset Store

https://assetstore.unity.com/packages/tools/integration/lock-task-211724

Window Incets Controller For Unity v.1.7

Supported OS : Android

32 & 64 bit Android 8.0+ (Sdk 26)

If you need any help, please contact me at amaiichigopurin@gmail.com

 

// how to start

 

  1. Add WindowIncetsController prefab to your scene
  2. Configure default settings in the editor inspector

 

C# Code Reference

// core methods

 

  • WindowIncetsControlCenter.Init(bool StatusBar, bool NavigationBar, bool LightStatusBar, bool LightNavigationBar)

Initiate the plugin with default states of system bars.

💡 You can automatically initiate the plugin with WindowIncetsController.cs



// C# public variables

 

  • bool WindowIncetsController.StatusBar  

Set or get status bar state. Set this True to enable the status bar.

💡 WindowIncetsController.StatusBar = true; (enables the status bar)

 

  • bool WindowIncetsController.NavigationBar  

Set or get navigation bar state. Set this True to enable the navigation bar.

💡 WindowIncetsController.NavigationBar = true; (enables the navigation bar)

 

  • bool WindowIncetsController.LightStatusBar  

Set or get navigation bar light mode state. Set this True to enable light mode (darkens the items on the status bar)

💡 WindowIncetsController.LightStatusBar = true; (darkens the items on the status bar)

  • bool WindowIncetsController.LightNavigationBar  

Set or get navigation bar light mode state. Set this True to enable light mode (darkens the items on the navigation bar)

💡 WindowIncetsController.LightNavigationBar = true; (darkens the items on the navigation bar)

 

  • Color WindowIncetsController.StatusBarColor

Set or get color of the status bar.

💡 WindowIncetsController.StatusBarColor = new Color(0, 0, 0, 0); (makes the status bar transparent)

 

  • Color WindowIncetsController.NavigationBarColor

Set or get color of the navigation bar.

💡 WindowIncetsController.NavigationBarColor = Color.Red; (makes the navigation bar red)

 

  • Int WindowIncetsController.GetStatusBarHeight

Returns height of the status bar.

 

  • Int WindowIncetsController.GetNavigationBarHeight

Returns height of the navigation bar.