Skip to content

Commit 4e9bc95

Browse files
authored
feat(music-controls): add music controls plugin support (#494)
1 parent ad57733 commit 4e9bc95

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { NativeStorage } from './plugins/nativestorage';
7070
import { Market } from './plugins/market';
7171
import { MediaPlugin } from './plugins/media';
7272
import { Mixpanel } from './plugins/mixpanel';
73+
import { MusicControls } from './plugins/music-controls';
7374
import { Network } from './plugins/network';
7475
import { NFC } from './plugins/nfc';
7576
import { OneSignal } from './plugins/onesignal';
@@ -181,6 +182,7 @@ InAppPurchase,
181182
Insomnia,
182183
Instagram,
183184
Keyboard,
185+
MusicControls,
184186
NativeAudio,
185187
NativeStorage,
186188
Network,
@@ -271,6 +273,7 @@ window['IonicNative'] = {
271273
MediaCapture: MediaCapture,
272274
MediaPlugin: MediaPlugin,
273275
Mixpanel: Mixpanel,
276+
MusicControls: MusicControls,
274277
NativeAudio: NativeAudio,
275278
NativePageTransitions: NativePageTransitions,
276279
NativeStorage: NativeStorage,

src/plugins/music-controls.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import {Plugin, Cordova} from './plugin';
2+
import {Observable} from 'rxjs/Observable';
3+
/**
4+
* @name MusicControls
5+
* @description
6+
* Music controls for Cordova applications.
7+
* Display a 'media' notification with play/pause, previous, next buttons, allowing the user to control the play.
8+
* Handle also headset event (plug, unplug, headset button).
9+
*
10+
* @usage
11+
* ```
12+
* import {MusicControls} from 'ionic-native';
13+
*
14+
* MusicControls.create({
15+
* track : 'Time is Running Out', // optional, default : ''
16+
* artist : 'Muse', // optional, default : ''
17+
* cover : 'albums/absolution.jpg', // optional, default : nothing
18+
* // 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)
19+
* // or a remote url ('http://...', 'https://...', 'ftp://...')
20+
* isPlaying : true, // optional, default : true
21+
* dismissable : true, // optional, default : false
22+
*
23+
* // hide previous/next/close buttons:
24+
* hasPrev : false, // show previous button, optional, default: true
25+
* hasNext : false, // show next button, optional, default: true
26+
* hasClose : true, // show close button, optional, default: false
27+
*
28+
* // Android only, optional
29+
* // text displayed in the status bar when the notification (and the ticker) are updated
30+
* ticker : 'Now playing "Time is Running Out"'
31+
* });
32+
*
33+
* MusicControls.subscribe().subscribe(action => {
34+
*
35+
* switch(action) {
36+
* case 'music-controls-next':
37+
* // Do something
38+
* break;
39+
* case 'music-controls-previous':
40+
* // Do something
41+
* break;
42+
* case 'music-controls-pause':
43+
* // Do something
44+
* break;
45+
* case 'music-controls-play':
46+
* // Do something
47+
* break;
48+
* case 'music-controls-destroy':
49+
* // Do something
50+
* break;
51+
*
52+
* // Headset events (Android only)
53+
* case 'music-controls-media-button' :
54+
* // Do something
55+
* break;
56+
* case 'music-controls-headset-unplugged':
57+
* // Do something
58+
* break;
59+
* case 'music-controls-headset-plugged':
60+
* // Do something
61+
* break;
62+
* default:
63+
* break;
64+
* }
65+
*
66+
* });
67+
*
68+
* MusicControls.listen(); // activates the observable above
69+
*
70+
* MusicControls.updateIsPlaying(true);
71+
*
72+
*
73+
* ```
74+
*/
75+
@Plugin({
76+
plugin: 'cordova-plugin-music-controls',
77+
pluginRef: 'MusicControls',
78+
repo: 'https://github.com/homerours/cordova-music-controls-plugin'
79+
})
80+
export class MusicControls {
81+
/**
82+
* Create the media controls
83+
* @param options {MusicControlsOptions}
84+
* @returns {Promise<any>}
85+
*/
86+
@Cordova()
87+
static create(options: MusicControlsOptions): Promise<any> {return; }
88+
89+
/**
90+
* Destroy the media controller
91+
* @returns {Promise<any>}
92+
*/
93+
@Cordova()
94+
static destroy(): Promise<any> {return; }
95+
96+
/**
97+
* Subscribe to the events of the media controller
98+
* @returns {Observable<any>}
99+
*/
100+
@Cordova({
101+
observable: true
102+
})
103+
static subscribe(): Observable<any> {return; }
104+
105+
/**
106+
* Start listening for events, this enables the Observable from the subscribe method
107+
*/
108+
@Cordova({sync: true})
109+
static listen(): void {}
110+
111+
/**
112+
* Toggle play/pause:
113+
* @param isPlaying {boolean}
114+
*/
115+
@Cordova({sync: true})
116+
static updateIsPlaying(isPlaying: boolean): void {}
117+
}
118+
export interface MusicControlsOptions {
119+
track: string;
120+
artist: string;
121+
cover: string;
122+
isPlaying: boolean;
123+
dismissable: boolean;
124+
hasPrev: boolean;
125+
hasNext: boolean;
126+
hasClose: boolean;
127+
ticker: string;
128+
}

0 commit comments

Comments
 (0)