使用react native製作的一款網絡音樂播放器 http://www.cnblogs.com/shaoting/p/6705307.html
基於第三方庫 react-native-video 設計
"react-native-video": "^1.0.0"
播放/暫停
快進/快退
循環模式(單曲,隨機,列表)
歌詞同步
進度條顯示
播放時間
基本旋轉動畫
動畫bug
安卓歌詞解析失敗
其他
使用的數據是百度音樂
http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=10&offset=0 //總列表
http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.lry&songid=213508 //歌詞文件
http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=877578 //播放
更多:http://67zixue.com/home/article/detail/id/22.html
主要代碼
把秒數轉換爲時間類型:
1
2
3
4
5
6
7
8
9
|
//把秒數轉換爲時間類型
formatTime(time) {
// 71s -> 01:11
let
min = Math.floor(time / 60)
let
second = time - min * 60
min = min >= 10 ? min :
'0'
+ min
second = second >= 10 ? second :
'0'
+ second
return
min +
':'
+ second
}
|
歌詞:
1
|
[ti:陽光總在風雨後] [ar:許美靜] [al:都是夜歸人] [00:05.97]陽光總在風雨後 [00:14.31]演唱:許美靜......
|
拿到當前歌曲的歌詞後,如上,把這段字符截成一個這樣的數組
其算法如下:
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
|
let
lry = responseJson.lrcContent
let
lryAry = lry.split(
'\n'
)
//按照換行符切數組
lryAry.forEach(
function
(val, index) {
var
obj = {}
//用於存放時間
val = val.replace(/(^\s*)|(\s*$)/g,
''
)
//正則,去除前後空格
let
indeofLastTime = val.indexOf(
']'
)
// ]的下標
let
timeStr = val.substring(1, indeofLastTime)
//把時間切出來 0:04.19
let
minSec =
''
let
timeMsIndex = timeStr.indexOf(
'.'
)
// .的下標
if
(timeMsIndex !== -1) {
//存在毫秒 0:04.19
minSec = timeStr.substring(1, val.indexOf(
'.'
))
// 0:04.
obj.ms = parseInt(timeStr.substring(timeMsIndex + 1, indeofLastTime))
//毫秒值 19
}
else
{
//不存在毫秒 0:04
minSec = timeStr
obj.ms = 0
}
let
curTime = minSec.split(
':'
)
// [0,04]
obj.min = parseInt(curTime[0])
//分鐘 0
obj.sec = parseInt(curTime[1])
//秒鐘 04
obj.txt = val.substring(indeofLastTime + 1, val.length)
//歌詞文本: 留下脣印的嘴
obj.txt = obj.txt.replace(/(^\s*)|(\s*$)/g,
''
)
obj.dis =
false
obj.total = obj.min * 60 + obj.sec + obj.ms / 100
//總時間
if
(obj.txt.length > 0) {
lyrObj.push(obj)
}
})
|
歌詞顯示:
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
|
// 歌詞
renderItem() {
// 數組
var
itemAry = [];
for
(
var
i = 0; i < lyrObj.length; i++) {
var
item = lyrObj[i].txt
if
(
this
.state.currentTime.toFixed(2) > lyrObj[i].total) {
//正在唱的歌詞
itemAry.push(
<View key={i} style={styles.itemStyle}>
<Text style={{ color:
'blue'
}}> {item} </Text>
</View>
);
_scrollView.scrollTo({x: 0,y:(25 * i),animated:
false
});
}
else
{
//所有歌詞
itemAry.push(
<View key={i} style={styles.itemStyle}>
<Text style={{ color:
'red'
}}> {item} </Text>
</View>
)
}
}
return
itemAry;
}
|
其餘什麼播放/暫停.時間顯示,快進/快退,進度條都是根據react-native-video 而來.
完整代碼:
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/**
* Created by shaotingzhou on 2017/4/13.
*/
import
React, { Component } from
'react'
import
{
AppRegistry,
StyleSheet,
Dimensions,
Text,
Image,
View,
Slider,
TouchableOpacity,
ScrollView,
ActivityIndicator,
Animated,
Easing
} from
'react-native'
var
{width,height} = Dimensions.get(
'window'
);
import
Video from
'react-native-video'
var
lyrObj = []
// 存放歌詞
var
myAnimate;
// http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=10&offset=0 //總列表
// http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.lry&songid=213508 //歌詞文件
// http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=877578 //播放
export
default
class
Main
extends
Component {
constructor(props) {
super
(props);
this
.spinValue =
new
Animated.Value(0)
this
.state = {
songs: [],
//歌曲id數據源
playModel:1,
// 播放模式 1:列表循環 2:隨機 3:單曲循環
btnModel:require(
'./image/列表循環.png'
),
//播放模式按鈕背景圖
pic_small:
''
,
//小圖
pic_big:
''
,
//大圖
file_duration:0,
//歌曲長度
song_id:
''
,
//歌曲id
title:
''
,
//歌曲名字
author:
''
,
//歌曲作者
file_link:
''
,
//歌曲播放鏈接
songLyr:[],
//當前歌詞
sliderValue: 0,
//Slide的value
pause:
false
,
//歌曲播放/暫停
currentTime: 0.0,
//當前時間
duration: 0.0,
//歌曲時間
currentIndex:0,
//當前第幾首
isplayBtn:require(
'./image/播放.png'
)
//播放/暫停按鈕背景圖
}
}
//上一曲
prevAction = (index) =>{
this
.recover()
lyrObj = [];
if
(index == -1){
index =
this
.state.songs.length - 1
// 如果是第一首就回到最後一首歌
}
this
.setState({
currentIndex:index
//更新數據
})
this
.loadSongInfo(index)
//加載數據
}
//下一曲
nextAction = (index) =>{
this
.recover()
lyrObj = [];
if
(index == 10){
index = 0
//如果是最後一首就回到第一首
}
this
.setState({
currentIndex:index,
//更新數據
})
this
.loadSongInfo(index)
//加載數據
}
//換歌時恢復進度條 和起始時間
recover = () =>{
this
.setState({
sliderValue:0,
currentTime: 0.0
})
}
//播放模式 接收傳過來的當前播放模式 this.state.playModel
playModel = (playModel) =>{
playModel++;
playModel = playModel == 4 ? 1 : playModel
//重新設置
this
.setState({
playModel:playModel
})
//根據設置後的模式重新設置背景圖片
if
(playModel == 1){
this
.setState({
btnModel:require(
'./image/列表循環.png'
),
})
}
else
if
(playModel == 2){
this
.setState({
btnModel:require(
'./image/隨機.png'
),
})
}
else
{
this
.setState({
btnModel:require(
'./image/單曲循環.png'
),
})
}
}
//播放/暫停
playAction =() => {
this
.setState({
pause: !
this
.state.pause
})
//判斷按鈕顯示什麼
if
(
this
.state.pause ==
true
){
this
.setState({
isplayBtn:require(
'./image/播放.png'
)
})
}
else
{
this
.setState({
isplayBtn:require(
'./image/暫停.png'
)
})
}
}
//播放器每隔250ms調用一次
onProgress =(data) => {
let
val = parseInt(data.currentTime)
this
.setState({
sliderValue: val,
currentTime: data.currentTime
})
//如果當前歌曲播放完畢,需要開始下一首
if
(val ==
this
.state.file_duration){
if
(
this
.state.playModel == 1){
//列表 就播放下一首
this
.nextAction(
this
.state.currentIndex + 1)
}
else
if
(
this
.state.playModel == 2){
let
last =
this
.state.songs.length
//json 中共有幾首歌
let
random = Math.floor(Math.random() * last)
//取 0~last之間的隨機整數
this
.nextAction(random)
//播放
}
else
{
//單曲 就再次播放當前這首歌曲
this
.refs.video.seek(0)
//讓video 重新播放
_scrollView.scrollTo({x: 0,y:0,animated:
false
});
}
}
}
//把秒數轉換爲時間類型
formatTime(time) {
// 71s -> 01:11
let
min = Math.floor(time / 60)
let
second = time - min * 60
min = min >= 10 ? min :
'0'
+ min
second = second >= 10 ? second :
'0'
+ second
return
min +
':'
+ second
}
// 歌詞
renderItem() {
// 數組
var
itemAry = [];
for
(
var
i = 0; i < lyrObj.length; i++) {
var
item = lyrObj[i].txt
if
(
this
.state.currentTime.toFixed(2) > lyrObj[i].total) {
//正在唱的歌詞
itemAry.push(
<View key={i} style={styles.itemStyle}>
<Text style={{ color:
'blue'
}}> {item} </Text>
</View>
);
_scrollView.scrollTo({x: 0,y:(25 * i),animated:
false
});
}
else
{
//所有歌詞
itemAry.push(
<View key={i} style={styles.itemStyle}>
<Text style={{ color:
'red'
}}> {item} </Text>
</View>
)
}
}
return
itemAry;
}
// 播放器加載好時調用,其中有一些信息帶過來
onLoad = (data) => {
this
.setState({ duration: data.duration });
}
loadSongInfo = (index) => {
//加載歌曲
let
songid =
this
.state.songs[index]
let
url =
'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid='
+ songid
fetch(url)
.then((response) => response.json())
.then((responseJson) => {
let
songinfo = responseJson.songinfo
let
bitrate = responseJson.bitrate
this
.setState({
pic_small:songinfo.pic_small,
//小圖
pic_big:songinfo.pic_big,
//大圖
title:songinfo.title,
//歌曲名
author:songinfo.author,
//歌手
file_link:bitrate.file_link,
//播放鏈接
file_duration:bitrate.file_duration
//歌曲長度
})
//加載歌詞
let
url =
'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.lry&songid='
+ songid
fetch(url)
.then((response) => response.json())
.then((responseJson) => {
let
lry = responseJson.lrcContent
let
lryAry = lry.split(
'\n'
)
//按照換行符切數組
lryAry.forEach(
function
(val, index) {
var
obj = {}
//用於存放時間
val = val.replace(/(^\s*)|(\s*$)/g,
''
)
//正則,去除前後空格
let
indeofLastTime = val.indexOf(
']'
)
// ]的下標
let
timeStr = val.substring(1, indeofLastTime)
//把時間切出來 0:04.19
let
minSec =
''
let
timeMsIndex = timeStr.indexOf(
'.'
)
// .的下標
if
(timeMsIndex !== -1) {
//存在毫秒 0:04.19
minSec = timeStr.substring(1, val.indexOf(
'.'
))
// 0:04.
obj.ms = parseInt(timeStr.substring(timeMsIndex + 1, indeofLastTime))
//毫秒值 19
}
else
{
//不存在毫秒 0:04
minSec = timeStr
obj.ms = 0
}
let
curTime = minSec.split(
':'
)
// [0,04]
obj.min = parseInt(curTime[0])
//分鐘 0
obj.sec = parseInt(curTime[1])
//秒鐘 04
obj.txt = val.substring(indeofLastTime + 1, val.length)
//歌詞文本: 留下脣印的嘴
obj.txt = obj.txt.replace(/(^\s*)|(\s*$)/g,
''
)
obj.dis =
false
obj.total = obj.min * 60 + obj.sec + obj.ms / 100
//總時間
if
(obj.txt.length > 0) {
lyrObj.push(obj)
}
})
})
})
}
componentWillMount() {
//先從總列表中獲取到song_id保存
fetch(
'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=10&offset=0'
)
.then((response) => response.json())
.then((responseJson) => {
var
listAry = responseJson.song_list
var
song_idAry = [];
//保存song_id的數組
for
|