This API provides music controls for your applications. It displays a 'media' notification with play/pause, previous, next buttons, allowing the user to control the music reproduction. It handles also headset event (plug, unplug, headset button).
var eventsHandler = function (action) {
const message = JSON.parse(action).message;
switch (message) {
case 'music-controls-next':
console.log("called Next");
break;
case 'music-controls-previous':
console.log("called previous");
break;
case 'music-controls-pause':
console.log("called pause");
MusicControls.updateIsPlaying(false);
break;
case 'music-controls-play':
console.log("called play");
MusicControls.updateIsPlaying(true);
break;
case 'music-controls-destroy':
console.log("called destroy");
break;
case 'music-controls-skip-forward':
console.log("called skip-forward");
break;
case 'music-controls-skip-backward':
console.log("called skip-backward");
break;
// Headset events
// All media button events are listed below
case 'music-controls-media-button' :
console.log("called media button");
break;
case 'music-controls-headset-unplugged':
console.log("called headdset desconected");
break;
case 'music-controls-headset-plugged':
console.log("called headdset conected");
break;
default:
console.log(message);
break;
}
}
MusicControls.create({
track: 'Track 01', // optional, default : ''
artist: 'Jhon Doe', // optional, default : ''
cover: 'https://demo.hybridstore.com/imgs/music.jpg', // optional, default : nothing
isPlaying: true, // optional, default : true
dismissable: true, // optional, default : false
// hide previous/next/close buttons:
hasPrev: true, // show previous button, optional, default: true
hasNext: true, // show next button, optional, default: true
hasClose: true, // show close button, optional, default: false
// text displayed in the status bar when the notification (and the ticker) are updated
ticker: 'Now playing "Track 01"'
})
.then(()=>{
MusicControls.subscribe(eventsHandler);
MusicControls.listen(); // activates the observable above
MusicControls.updateIsPlaying(true);
});
var eventsHandler = function (action) {
const message = JSON.parse(action).message;
switch (message) {
case "music-controls-next":
console.log("called Next");
break;
case "music-controls-previous":
console.log("called previous");
break;
case "music-controls-pause":
console.log("called pause");
window.MusicControls.updateIsPlaying(false);
break;
case "music-controls-play":
console.log("called play");
window.MusicControls.updateIsPlaying(true);
break;
case "music-controls-destroy":
console.log("called destroy");
break;
case "music-controls-skip-forward":
console.log("called skip-forward");
break;
case "music-controls-skip-backward":
console.log("called skip-backward");
break;
// Headset events
// All media button events are listed below
case "music-controls-media-button":
console.log("called media button");
break;
case "music-controls-headset-unplugged":
console.log("called headdset desconected");
break;
case "music-controls-headset-plugged":
console.log("called headdset conected");
break;
default:
console.log(message);
break;
}
};
window.MusicControls.create({
track: "Track 01", // optional, default : ''
artist: "Jhon Doe", // optional, default : ''
cover: "https://demo.hybridstore.com/imgs/music.jpg", // optional, default : nothing
isPlaying: true, // optional, default : true
dismissable: true, // optional, default : false
// hide previous/next/close buttons:
hasPrev: true, // show previous button, optional, default: true
hasNext: true, // show next button, optional, default: true
hasClose: true, // show close button, optional, default: false
// text displayed in the status bar when the notification (and the ticker) are updated
ticker: 'Now playing "Track 01"',
})
.then(() => {
window.MusicControls.subscribe(eventsHandler);
window.MusicControls.listen(); // activates the observable above
window.MusicControls.updateIsPlaying(true);
});
var eventsHandler = function (action) {
const message = JSON.parse(action).message;
switch (message) {
case "music-controls-next":
console.log("called Next");
break;
case "music-controls-previous":
console.log("called previous");
break;
case "music-controls-pause":
console.log("called pause");
window.MusicControls.updateIsPlaying(false);
break;
case "music-controls-play":
console.log("called play");
window.MusicControls.updateIsPlaying(true);
break;
case "music-controls-destroy":
console.log("called destroy");
break;
case "music-controls-skip-forward":
console.log("called skip-forward");
break;
case "music-controls-skip-backward":
console.log("called skip-backward");
break;
// Headset events
// All media button events are listed below
case "music-controls-media-button":
console.log("called media button");
break;
case "music-controls-headset-unplugged":
console.log("called headdset desconected");
break;
case "music-controls-headset-plugged":
console.log("called headdset conected");
break;
default:
console.log(message);
break;
}
};
window.MusicControls.create({
track: "Track 01", // optional, default : ''
artist: "Jhon Doe", // optional, default : ''
cover: "https://demo.hybridstore.com/imgs/music.jpg", // optional, default : nothing
isPlaying: true, // optional, default : true
dismissable: true, // optional, default : false
// hide previous/next/close buttons:
hasPrev: true, // show previous button, optional, default: true
hasNext: true, // show next button, optional, default: true
hasClose: true, // show close button, optional, default: false
// text displayed in the status bar when the notification (and the ticker) are updated
ticker: 'Now playing "Track 01"',
})
.then(() => {
window.MusicControls.subscribe(eventsHandler);
window.MusicControls.listen(); // activates the observable above
window.MusicControls.updateIsPlaying(true);
});
var eventsHandler = function (action) {
const message = JSON.parse(action).message;
switch (message) {
case "music-controls-next":
console.log("called Next");
break;
case "music-controls-previous":
console.log("called previous");
break;
case "music-controls-pause":
console.log("called pause");
(<any>window).MusicControls.updateIsPlaying(false);
break;
case "music-controls-play":
console.log("called play");
(<any>window).MusicControls.updateIsPlaying(true);
break;
case "music-controls-destroy":
console.log("called destroy");
break;
case "music-controls-skip-forward":
console.log("called skip-forward");
break;
case "music-controls-skip-backward":
console.log("called skip-backward");
break;
// Headset events
// All media button events are listed below
case "music-controls-media-button":
console.log("called media button");
break;
case "music-controls-headset-unplugged":
console.log("called headdset desconected");
break;
case "music-controls-headset-plugged":
console.log("called headdset conected");
break;
default:
console.log(message);
break;
}
};
(<any>window).MusicControls.create({
track: "Track 01", // optional, default : ''
artist: "Jhon Doe", // optional, default : ''
cover: "https://demo.hybridstore.com/imgs/music.jpg", // optional, default : nothing
isPlaying: true, // optional, default : true
dismissable: true, // optional, default : false
// hide previous/next/close buttons:
hasPrev: true, // show previous button, optional, default: true
hasNext: true, // show next button, optional, default: true
hasClose: true, // show close button, optional, default: false
// text displayed in the status bar when the notification (and the ticker) are updated
ticker: 'Now playing "Track 01"',
})
.then(() => {
(<any>window).MusicControls.subscribe(eventsHandler);
(<any>window).MusicControls.listen(); // activates the observable above
(<any>window).MusicControls.updateIsPlaying(true);
});
var eventsHandler = function (action) {
const message = JSON.parse(action).message;
switch (message) {
case "music-controls-next":
console.log("called Next");
break;
case "music-controls-previous":
console.log("called previous");
break;
case "music-controls-pause":
console.log("called pause");
window.MusicControls.updateIsPlaying(false);
break;
case "music-controls-play":
console.log("called play");
window.MusicControls.updateIsPlaying(true);
break;
case "music-controls-destroy":
console.log("called destroy");
break;
case "music-controls-skip-forward":
console.log("called skip-forward");
break;
case "music-controls-skip-backward":
console.log("called skip-backward");
break;
// Headset events
// All media button events are listed below
case "music-controls-media-button":
console.log("called media button");
break;
case "music-controls-headset-unplugged":
console.log("called headdset desconected");
break;
case "music-controls-headset-plugged":
console.log("called headdset conected");
break;
default:
console.log(message);
break;
}
};
window.MusicControls.create({
track: "Track 01", // optional, default : ''
artist: "Jhon Doe", // optional, default : ''
cover: "https://demo.hybridstore.com/imgs/music.jpg", // optional, default : nothing
isPlaying: true, // optional, default : true
dismissable: true, // optional, default : false
// hide previous/next/close buttons:
hasPrev: true, // show previous button, optional, default: true
hasNext: true, // show next button, optional, default: true
hasClose: true, // show close button, optional, default: false
// text displayed in the status bar when the notification (and the ticker) are updated
ticker: 'Now playing "Track 01"',
})
.then(() => {
window.MusicControls.subscribe(eventsHandler);
window.MusicControls.listen(); // activates the observable above
window.MusicControls.updateIsPlaying(true);
});
}
updateIsPlaying(option: boolean): Promise <any>
Toggle play/pause button
updateDismissable(option: boolean): Promise <any>
interface CreateOptions { track ? : string; // Track name artist ? : string; // Artist name album ? : string; // Album name cover ? : string; // local path or a remote url ('http://...', 'https://...', 'ftp://...') isPlaying ? : boolean; // default : true dismissable ? : boolean; // default : false hasPrev ? : boolean; // hide previous, default : true hasNext ? : boolean; // hide previous next, default : true hasClose ? : boolean; // hide close, default : false ticker ? : string;// text displayed in the status bar when the notification (and the ticker) are updated }
interface EventObject {
message: string; // one of Action
}
enum Action { "music-controls-media-button-next", "music-controls-media-button-pause", "music-controls-media-button-play", "music-controls-media-button-play-pause", "music-controls-media-button-previous", "music-controls-media-button-stop", "music-controls-media-button-fast-forward", "music-controls-media-button-rewind", "music-controls-media-button-skip-backward", "music-controls-media-button-skip-forward", "music-controls-media-button-step-backward", "music-controls-media-button-step-forward", "music-controls-media-button-meta-left", "music-controls-media-button-meta-right", "music-controls-media-button-music", "music-controls-media-button-volume-up", "music-controls-media-button-volume-down", "music-controls-media-button-volume-mute", "music-controls-media-button-headset-hook" }