-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
269 lines (223 loc) · 5.48 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* @providesModule RNAudio
* @flow
*/
'use strict';
import React, { Component } from 'react';
import {
NativeModules,
NativeEventEmitter,
Platform,
} from 'react-native';
// //GUI ifused as a React Native Component
import Gui from './gui';
import TimerMixin from 'react-timer-mixin';
//Bridge Objective-c
const { NativeRNAudioPlayer } = NativeModules;
const NativeRNAudioEmitter = new NativeEventEmitter(NativeRNAudioPlayer);
let enableBackgroundModeIsSet = false;
class AudioPlayer extends Gui {
constructor( props ) {
super( props );
this._isComponent = false,
this.state = Object.assign ({
//Public vars
src: null,
cover: null,
//Callbacks
autoplay: false,
canplay: null,
timeupdate: null,
ended: null,
//Private vars
_isPlaying: 0,
_currentTime: 0,
_duration: 0,
_loop: false,
_isMuted: false,
_isEnded: false,
_key: null,
_isLoaded: false,
_isReadyToPlay: false,
_isFinished: false,
}, {}, props);
this.subscription = NativeRNAudioEmitter.addListener(
'PlayerUpdate',
( data ) => {
if(this.state._key == data._key){
this.state.timeupdate && this.state.timeupdate(data._currentTime);
this.state.playing && (this.state._isPlaying != data._isPlaying) && this.state.playing( data._isPlaying ? true : false );
this._setState( {
_currentTime: data._currentTime,
_isPlaying: data._isPlaying,
});
if(data._isFinished && data._isFinished !== this._isFinished ) {
this.state.ended && this.state.ended();
this._isFinished = true;
} else if(data._isReadyToPlay && data._isReadyToPlay !== this._isReadyToPlay ) {
this.state.canplay && this.state.canplay();
this._isReadyToPlay = true;
}
}
}
);
}
componentDidMount() {
this._isComponent = true;
this._setState( {
ended: () => this.stop()
});
this._prepare();
}
componentWillUnmount() {
this.subscription.remove();
}
//Public params
set src ( string ) {
this.state.src = string;
// Wait until al te protoypes are set
TimerMixin.setTimeout(() => {
this._prepare();
});
}
//Airplay data
set title ( string ) {
this._setState( {
title: string,
});
}
set artist ( string ) {
this._setState( {
artist: string,
});
}
set album ( string ) {
this._setState( {
album: string,
});
}
set cover ( string ) {
this._setState( {
cover: string,
});
}
//Callback functions
set timeupdate ( callback ) {
this._setState( {
timeupdate: callback,
});
}
set canplay ( callback ) {
this._setState( {
canplay: callback,
});
}
set playing ( callback ) {
this._setState( {
playing: callback,
});
}
//Params
set ended ( callback ) {
this.state.ended = callback;
}
set loop ( bool ) {
this.state.loop = bool;
}
get muted () {
return this.state._isMuted;
}
set muted ( bool ) {
NativeRNAudioPlayer.muted( this.state._key, bool );
}
get loop () {
return this.state._loop;
}
get currentTime () {
return this.state._currentTime;
}
set currentTime ( seconds ) {
NativeRNAudioPlayer.seek( this.state._key, seconds );
}
get duration () {
return this.state._duration;
}
get paused () {
return !this.state._isPlaying;
}
//controls
play () {
!this.state._isLoaded && this._prepare();
this._isFinished = false;
NativeRNAudioPlayer.play( this.state._key );
}
pause () {
console.log("isNotPlaying");
NativeRNAudioPlayer.pause( this.state._key );
}
stop () {
console.log("isStopped");
this.currentTime = 0;
NativeRNAudioPlayer.stop( this.state._key );
}
//Private fuctions
_prepare() {
!enableBackgroundModeIsSet && NativeRNAudioPlayer.enableBackgroundMode();
enableBackgroundModeIsSet = true;
if(!this.state.src) {
return;
}
this.state._key = Math.floor(Math.random() * 10000000);
//Get options
var options = Object.keys(this.state).reduce((previous, current) => {
if(['artist','title','album','cover'].indexOf(current) !== -1 && this.state[current]){
previous[current] = this.state[current];
}
return previous;
}, {});
NativeRNAudioPlayer.prepare(
this.state.src,
this.state._key,
options,
( data ) => {
this.state._duration = data._duration;
}
);
this.state._isLoaded = true;
}
_djb2Code(str) {
var hash = 5381, i, char;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char;
}
return hash;
}
_setState( data ) {
if(this._isComponent) {
this.setState( data );
} else {
this.state = Object.assign(this.state, data)
}
}
}
let AudioPlayerUtils = {};
if (Platform.OS === 'ios') {
AudioPlayerUtils = {
MainBundlePath: NativeRNAudioPlayer.MainBundlePath,
CachesDirectoryPath: NativeRNAudioPlayer.NSCachesDirectoryPath,
DocumentDirectoryPath: NativeRNAudioPlayer.NSDocumentDirectoryPath,
LibraryDirectoryPath: NativeRNAudioPlayer.NSLibraryDirectoryPath,
};
} else if (Platform.OS === 'android') {
// AudioUtils = {
// MainBundlePath: NativeRNAudioPlayer.MainBundlePath,
// CachesDirectoryPath: NativeRNAudioPlayer.CachesDirectoryPath,
// DocumentDirectoryPath: NativeRNAudioPlayer.DocumentDirectoryPath,
// LibraryDirectoryPath: NativeRNAudioPlayer.LibraryDirectoryPath,
// PicturesDirectoryPath: NativeRNAudioPlayer.PicturesDirectoryPath,
// MusicDirectoryPath: NativeRNAudioPlayer.MusicDirectoryPath,
// DownloadsDirectoryPath: NativeRNAudioPlayer.DownloadsDirectoryPath
// };
}
module.exports = {AudioPlayer, AudioPlayerUtils};