-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
124 lines (112 loc) · 4.59 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {NativeModules, Platform, PermissionsAndroid} from 'react-native'
// to transpile to es5 for web: https://babeljs.io/repl/
// check off es2015, react
class MusicPlayerController {
static async requestPermission(title, message, grantedHandler, previouslyGrantedHandler, declinedHandler) {
const key = PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE
if (Platform.OS == 'android') {
try {
const preGranted = await PermissionsAndroid.check(key)
if (preGranted) {
previouslyGrantedHandler()
} else {
try {
const granted = await PermissionsAndroid.request( key, {'title': title, 'message': message})
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
grantedHandler()
} else {
declinedHandler()
}
} catch (err) {
console.warn(err)
}
}
} catch (error) {
}
} else {
previouslyGrantedHandler()
}
}
static presentPicker(webSaveToLocalStorage, successHandler, cancelHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.presentPicker((errorCode, metadata) => {
if (errorCode == 0) {
// No Error. User picked
successHandler(metadata)
} else if (errorCode == 1) {
// Able to open Picker, but user tapped Cancel
cancelHandler()
} else if (errorCode == 2) {
// Cannot open picker - Using Simulator
alert("Sorry - iOS doesn't allow MPMusicPlayerController to be opened on the simulator. Please try on an actual device with at least one song in the library (song download from iTunes and in your Music App)")
}
})
}
static preloadMusic(repeatMode, successHandler, errorHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.preloadMusic(repeatMode, (errorCode, metadata) => {
if (errorCode == 0) {
// Preload successful (application music player)
successHandler(metadata)
} else if (errorCode == 1) {
// No music to preload (nothing in NSUserDefaults)
errorHandler()
}
})
}
static playMusic(successHandler, errorHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.playMusic((errorCode, metadata) => {
if (errorCode == 0) {
// Successfully played music (even if already playing)
successHandler()
} else if (errorCode == 1) {
// Unable to play music. Did you preload it?
errorHandler()
}
})
}
static stopMusic(successHandler, errorHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.stopMusic((errorCode, metadata) => {
if (errorCode == 0) {
// Successfully stopped music (even if it was already stopped)
successHandler()
} else if (errorCode == 1) {
// Unable to stop music. Did you preload it?
errorHandler()
}
})
}
static pauseMusic(successHandler, errorHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.pauseMusic((errorCode, metadata) => {
if (errorCode == 0) {
// Successfully stopped music (even if it was already stopped)
successHandler()
} else if (errorCode == 1) {
// Unable to stop music. Did you preload it?
errorHandler()
}
})
}
static isPlaying(isPlayingHandler, notPlayingHandler) {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.isPlaying((errorCode, result) => {
if (errorCode == 0) {
// Music Player is playing
isPlayingHandler()
} else if (errorCode == 1) {
// Not playing
notPlayingHandler()
}
})
}
static setSessionCategory(category, option) {
if (Platform.OS == 'ios') {
const player = NativeModules.RNReactNativeMusicplayercontroller
player.setSessionCategory(category, option)
}
}
}
export default MusicPlayerController