MusicControls

Improve this doc

$ ionic plugin add cordova-plugin-music-controls

Repo: https://github.com/homerours/cordova-music-controls-plugin

Music controls for Cordova applications. Display a 'media' notification with play/pause, previous, next buttons, allowing the user to control the play. Handle also headset event (plug, unplug, headset button).

Usage

import {MusicControls} from 'ionic-native';

MusicControls.create({
  track       : 'Time is Running Out',        // optional, default : ''
  artist      : 'Muse',                       // optional, default : ''
  cover       : 'albums/absolution.jpg',      // optional, default : nothing
  // cover can be a local path (use fullpath 'file:///storage/emulated/...', or only 'my_image.jpg' if my_image.jpg is in the www folder of your app)
  //           or a remote url ('http://...', 'https://...', 'ftp://...')
  isPlaying   : true,                         // optional, default : true
  dismissable : true,                         // optional, default : false

  // hide previous/next/close buttons:
  hasPrev   : false,      // show previous button, optional, default: true
  hasNext   : false,      // show next button, optional, default: true
  hasClose  : true,       // show close button, optional, default: false

  // Android only, optional
  // text displayed in the status bar when the notification (and the ticker) are updated
  ticker    : 'Now playing "Time is Running Out"'
 });

 MusicControls.subscribe().subscribe(action => {

   switch(action) {
       case 'music-controls-next':
           // Do something
           break;
       case 'music-controls-previous':
           // Do something
           break;
       case 'music-controls-pause':
           // Do something
           break;
       case 'music-controls-play':
           // Do something
           break;
       case 'music-controls-destroy':
           // Do something
           break;

       // Headset events (Android only)
       case 'music-controls-media-button' :
           // Do something
           break;
       case 'music-controls-headset-unplugged':
           // Do something
           break;
       case 'music-controls-headset-plugged':
           // Do something
           break;
       default:
           break;
   }

 });

 MusicControls.listen(); // activates the observable above

 MusicControls.updateIsPlaying(true);

Static Members

create(options)

Create the media controls

Param Type Details
options MusicControlsOptions

Returns: Promise<any>

destroy()

Destroy the media controller

Returns: Promise<any>

subscribe()

Subscribe to the events of the media controller

Returns: Observable<any>

listen()

Start listening for events, this enables the Observable from the subscribe method

updateIsPlaying(isPlaying)

Toggle play/pause:

Param Type Details
isPlaying boolean

MusicControlsOptions

Param Type Details
track string
artist string
cover string
isPlaying boolean
dismissable boolean
hasPrev boolean
hasNext boolean
hasClose boolean
ticker string

API

Native

General