使用react native制作的一款网络音乐播放器

使用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 ( var  i = 0;i<listAry.length;i++){
                       let  song_id = listAry[i].song_id
                       song_idAry.push(song_id)
                   }
                 this .setState({
                     songs:song_idAry
                 })
                 this .loadSongInfo(0)    //预先加载第一首
             })
 
         this .spin()    //   启动旋转
 
     }
 
     //旋转动画
     spin () {
         this .spinValue.setValue(0)
         myAnimate = Animated.timing(
             this .spinValue,
             {
                 toValue: 1,
                 duration: 4000,
                 easing: Easing.linear
             }
         ).start(() =>  this .spin())
 
     }
 
     render() {
         //如果未加载出来数据 就一直转菊花
         if  ( this .state.file_link.length <= 0 ) {
             return (
                 <ActivityIndicator
                     animating={ this .state.animating}
                     style={{flex: 1,alignItems:  'center' ,justifyContent:  'center' }}
                     size= "large"  />
             )
         } else {
             const spin =  this .spinValue.interpolate({
                 inputRange: [0, 1],
                 outputRange: [ '0deg' '360deg' ]
             })
 
 
             //数据加载出来
             return  (
                 <View style={styles.container}>
                     { /*背景大图*/ }
                     <Image source={{uri: this .state.pic_big}} style={{flex:1}}/>
                     { /*背景白色透明遮罩*/ }
                     <View style = {{position: 'absolute' ,width: width,height:height,backgroundColor: 'white' ,opacity:0.8}}/>
 
                     <View style = {{position: 'absolute' ,width: width}}>
                         { /*胶片光盘*/ }
                         <Image source={require( './image/胶片盘.png' )} style={{width:220,height:220,alignSelf: 'center' }}/>
 
                         { /*旋转小图*/ }
                         <Animated.Image
                             ref =  'myAnimate'
                             style={{width:140,height:140,marginTop: -180,alignSelf: 'center' ,borderRadius: 140*0.5,transform: [{rotate: spin}]}}
                             source={{uri:  this .state.pic_small}}
                         />
 
                         { /*播放器*/ }
                         <Video
                             source={{uri:  this .state.file_link}}
                             ref= 'video'
                             volume={1.0}
                             paused={ this .state.pause}
                             onProgress={(e) =>  this .onProgress(e)}
                             onLoad={(e) =>  this .onLoad(e)}
                         />
                         { /*歌曲信息*/ }
                         <View style={styles.playingInfo}>
                             { /*作者-歌名*/ }
                             <Text>{ this .state.author} - { this .state.title}</Text>
                             { /*时间*/ }
                             <Text>{ this .formatTime(Math.floor( this .state.currentTime))} - { this .formatTime(Math.floor( this .state.duration))}</Text>
                         </View>
                         { /*播放模式*/ }
                         <View style = {{marginTop: 5,marginBottom:5,marginLeft: 20}}>
                             <TouchableOpacity onPress={()=> this .playModel( this .state.playModel)}>
                                 <Image source={ this .state.btnModel} style={{width:20,height:20}}/>
                             </TouchableOpacity>
                         </View>
                         { /*进度条*/ }
                         <Slider
                             ref= 'slider'
                             style={{ marginLeft: 10, marginRight: 10}}
                             value={ this .state.sliderValue}
                             maximumValue={ this .state.file_duration}
                             step={1}
                             minimumTrackTintColor= '#FFDB42'
                             onValueChange={(value) => {
                               this .setState({
                                   currentTime:value
                               })
                                         }
                                     }
                             onSlidingComplete={(value) => {
                                          this .refs.video.seek(value)
                                     }}
                         />
                         { /*歌曲按钮*/ }
                         <View style = {{flexDirection: 'row' ,justifyContent: 'space-around' }}>
                             <TouchableOpacity onPress={()=> this .prevAction( this .state.currentIndex - 1)}>
                                 <Image source={require( './image/上一首.png' )} style={{width:30,height:30}}/>
                             </TouchableOpacity>
 
                             <TouchableOpacity onPress={()=> this .playAction()}>
                                 <Image source={ this .state.isplayBtn} style={{width:30,height:30}}/>
                             </TouchableOpacity>
 
                             <TouchableOpacity onPress={()=> this .nextAction( this .state.currentIndex + 1)}>
                                 <Image source={require( './image/下一首.png' )} style={{width:30,height:30}}/>
                             </TouchableOpacity>
                         </View>
 
                         { /*歌词*/ }
                         <View style={{height:140,alignItems: 'center' }}>
 
                             <ScrollView style={{position: 'relative' }}
                                         ref={(scrollView) => { _scrollView = scrollView}}
                             >
                                 { this .renderItem()}
                             </ScrollView>
                         </View>
                     </View>
 
                 </View>
             )
         }
 
     }
}
 
const styles = StyleSheet.create({
     container: {
         flex: 1,
     },
     image: {
         flex: 1
     },
     playingControl: {
         flexDirection:  'row' ,
         alignItems:  'center' ,
         paddingTop: 10,
         paddingLeft: 20,
         paddingRight: 20,
         paddingBottom: 20
     },
     playingInfo: {
         flexDirection:  'row' ,
         alignItems: 'stretch' ,
         justifyContent:  'space-between' ,
         paddingTop: 40,
         paddingLeft: 20,
         paddingRight: 20,
         backgroundColor: 'rgba(255,255,255,0.0)'
     },
     text: {
         color:  "black" ,
         fontSize: 22
     },
     modal: {
         height: 300,
         borderTopLeftRadius: 5,
         borderTopRightRadius: 5,
         paddingTop: 5,
         paddingBottom: 50
     },
     itemStyle: {
         paddingTop: 20,
         height:25,
         backgroundColor: 'rgba(255,255,255,0.0)'
     }
})

  

 

 

github地址: https://github.com/pheromone/react-native-videoDemo

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值