Music Player

Before you begin, make sure that Media Plater service is enabled under the plugin Settings.

You also need to enable Music Player Controller API and fill in the Media Library Usage Descitbiont. 

Overview

Create an instance of a music player to play media items in your app. There are two types of music player:

  • An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it affect the Music app’s state. There are two application music players: ApplicationMusicPlayer and ApplicationQueuePlayer. The application queue player provides greater control over the contents of the queue and is the preferred player.

  • The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The Music app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.

Use Example

First of all, you need to create an instance of ISN_MPMusicPlayerController, you can use one of the 3 options, based on your application needs. The ISN_MPMusicPlayerController class is based on iOS MPMusicPlayerController class. Let us know if there is some feature that you are missing comparing to full iOS API

System music player 

using SA.iOS.MediaPlayer;
...

var player = ISN_MPMusicPlayerController.SystemMusicPlayer;

Application music player 

using SA.iOS.MediaPlayer;
...

var player = ISN_MPMusicPlayerController.ApplicationMusicPlayer;

Application queue player 

using SA.iOS.MediaPlayer;
...

var player = ISN_MPMusicPlayerController.ApplicationQueuePlayer;

Once you have an ISN_MPMusicPlayerController instance you can control playback using the class available methods:

public class ISN_MPMusicPlayerController {

    //--------------------------------------
    // Public Methods
    //--------------------------------------

        
    /// <summary>
    /// Play current player track
    /// </summary>
    public void Play();


    /// <summary>
    /// Stop current player track
    /// </summary>
    public void Stop();


    /// <summary>
    /// Pause current player track
    /// </summary>
    public void Pause();

    /// <summary>
    /// Starts playback of the next media item in the playback queue; 
    /// or, the music player is not playing, designates the next media item as the next to be played.
    /// </summary>
    public void SkipToNextItem();


    /// <summary>
    /// Starts playback of the previous media item in the playback queue; 
    /// or, the music player is not playing, designates the previous media item as the next to be played.
    /// </summary>
    public void SkipToPreviousItem();

    /// <summary>
    /// Sets a music player's playback queue using with media items identified by the store identifiers.
    /// </summary>
    /// <param name="storeIDs">An array of store identifiers associated with the media items to be added to the queue.</param>
    public void SetQueueWithStoreIDs(params string[] storeIDs);


    //--------------------------------------
    // Get / Set
    //--------------------------------------

    /// <summary>
    /// You determine a music player’s state by checking the playbackState property. 
    /// Depending on the property’s value, 
    /// you can update your application’s user interface or take other appropriate action.
    /// </summary>
    public ISN_MPMusicPlaybackState PlaybackState {get;}


    /// <summary>
    /// The currently-playing media item, or the media item, 
    /// within a queue, that you have designated to begin playback with.
    /// </summary>
    public ISN_MPMediaItem NowPlayingItem  {get;}

}  

Current Track Info

The current track info is represented as ISN_MPMediaItem object which is based on MPMediaItem native class. Let us know if there is some feature that you are missing compared to MPMediaItem. The code snippet below shows ho yo retrieve information of current track from the player. 

using SA.iOS.MediaPlayer;
...

var player = ISN_MPMusicPlayerController.SystemMusicPlayer;
ISN_MPMediaItem item = player.NowPlayingItem;

Debug.Log("item.Title: " + item.Title);
Debug.Log("item.Artist: " + item.Artist);
Debug.Log("item.AlbumTitle: " + item.AlbumTitle);
Debug.Log("item.Composer: " + item.Composer);
Debug.Log("item.Genre: " + item.Genre);
Debug.Log("item.Lyrics: " + item.Lyrics);

Playback Notifications

You can subscribe to the media player playback notification via the Notification Center. You also might want to begin generating playback notifications using the BeginGeneratingPlaybackNotifications method. The code snippet below dementates hot to subscribe to the NowPlayingItemDidChange and PlaybackStateDidChange player events.

using SA.iOS.Foundation;
using SA.iOS.MediaPlayer;
...

var player = ISN_MPMusicPlayerController.SystemMusicPlayer;
player.BeginGeneratingPlaybackNotifications();

var center = ISN_NSNotificationCenter.DefaultCenter;
center.AddObserverForName(ISN_MPMusicPlayerController.NowPlayingItemDidChange, 
    (ISN_NSNotification notification) => {
        Debug.Log("MusicPlayer Now Playing Item Did Change");
});


center.AddObserverForName(ISN_MPMusicPlayerController.PlaybackStateDidChange,
   (ISN_NSNotification notification) => {
       Debug.Log("MusicPlayer Playback State Did Change");
});