Skip to content

Music Player

Stanislav Osipov edited this page Apr 18, 2020 · 5 revisions

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

MusicPlayer

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

MusicPlayer2

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.

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 the 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");
});

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally