forked from kadiks/react-native-itunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itunes.js
125 lines (110 loc) · 2.65 KB
/
itunes.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
var React = require("react-native");
var { RNiTunes } = require("react-native").NativeModules;
module.exports = {
getPlaylists: function(params) {
return new Promise(resolve => {
RNiTunes.getPlaylists(params || {}, playlists => {
resolve(playlists);
});
});
},
getTracks: function(params) {
return new Promise(resolve => {
RNiTunes.getTracks(params || {}, tracks => {
resolve(tracks);
});
});
},
getArtists: function(params) {
return new Promise(resolve => {
RNiTunes.getArtists(params || {}, tracks => {
resolve(tracks);
});
});
},
getAlbums: function(params) {
return new Promise(resolve => {
RNiTunes.getAlbums(params || {}, tracks => {
resolve(tracks);
});
});
},
getCurrentPlayTime: function(params) {
return new Promise(resolve => {
RNiTunes.getCurrentPlayTime(currentPlayTime => {
resolve(currentPlayTime);
});
});
},
getCurrentTrack: function() {
return new Promise((resolve, reject) => {
RNiTunes.getCurrentTrack((err, track) => {
if (!err) {
resolve(track);
}
reject(err);
});
});
},
pause: function() {
RNiTunes.pause();
},
play: function() {
RNiTunes.play();
},
previous: function() {
RNiTunes.previous();
},
next: function() {
RNiTunes.next();
},
playTrack: function(trackItem) {
return new Promise((resolve, reject) => {
if (
!trackItem.hasOwnProperty("title") ||
!trackItem.hasOwnProperty("albumTitle")
) {
reject(
"To play a track, you need to send the [title] and the [albumTitle] properties"
);
return;
}
RNiTunes.playTrack(trackItem || {}, err => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
},
playTracks: function(trackItems) {
return new Promise((resolve, reject) => {
if (Array.isArray(trackItems) === false || trackItems.length === 0) {
reject("No track item have been found");
return;
}
const isValid = trackItems.every(
t => t.hasOwnProperty("title") && t.hasOwnProperty("albumTitle")
);
if (isValid === false) {
reject(
"All track items should have [title] and [albumTitle] properties"
);
}
RNiTunes.playTracks(trackItems || [], err => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
},
seekTo: function(playingTime) {
RNiTunes.seekTo(playingTime);
},
stop: function() {
RNiTunes.stop();
}
};