|
| 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