forked from tanguyantoine/react-native-music-control
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ios.js
55 lines (51 loc) · 1.32 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @providesModule MusicControl
* @flow
*/
'use strict';
import { NativeModules, DeviceEventEmitter } from 'react-native';
const NativeMusicControl = NativeModules.MusicControlManager;
/**
* High-level docs for the MusicControl iOS API can be written here.
*/
var handlers = { };
var subscription = null;
var MusicControl = {
enableBackgroundMode: function(enable){
NativeMusicControl.enableBackgroundMode(enable)
},
setNowPlaying: function(info){
NativeMusicControl.setNowPlaying(info)
},
resetNowPlaying: function(){
NativeMusicControl.resetNowPlaying()
},
enableControl: function(controlName, bool, options = {}){
NativeMusicControl.enableControl(controlName, bool, options || {})
},
handleCommand: function(commandName){
if(handlers[commandName]){
handlers[commandName]()
}
},
on: function(actionName, cb){
if(subscription){
subscription.remove();
}
subscription = DeviceEventEmitter.addListener(
'RNMusicControlEvent',
(event) => {
MusicControl.handleCommand(event.name)
}
);
handlers[actionName] = cb
},
off: function(actionName, cb){
delete(handlers[actionName])
if(!Object.keys(handlers).length && subscription){
subscription.remove()
subscription = null;
}
}
};
module.exports = MusicControl;