React Native (一) react-native-video實現音樂播放器和進度條的功能
功能:
1.卡片滑動切歌
2.顯示進度條
效果圖:

第三方組件:
1.react-native-video
Github地址:https://github.com/react-native-community/react-native-video
2.react-native-animated-tabs
Github地址:https://github.com/philipshurpik/react-native-animated-tabs
下載方式:
Using npm:
npm install --save react-native-video
npm install --save react-native-animated-tabs
or using yarn:
yarn add react-native-video
yarn add react-native-animated-tabs
如果RN版本>=0.60,是自動link的,如果在0.60以下則需要手動link
運行 `react-native link react-native-video` 連接react-native-video庫
react-native-animated-tabs同上
卡片滑動功能
1.導入需要的組件
import AnimatedTabs from "react-native-animated-tabs";
2.使用組件
<AnimatedTabs
panelWidth={getPanelWidth()}
activePanel={this.state.activePanel}
onAnimateFinish={activePanel => this.setState({ activePanel })}
>
/**
此處放你的卡片
<View><Image source={...}/></View>
...
*/
</AnimatedTabs>
3.按鈕切換卡片
<View style={styles.buttons}>
<TouchableOpacity
style={styles.text}
onPress={() => this.goToPanel(-1)}
>
<Text>上一首歌</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.text}
onPress={() => this.goToPanel(1)}
>
<Text>下一首歌</Text>
</TouchableOpacity>
4.goToPanel()函數
goToPanel(direction) {
const nextPanel = this.state.activePanel + direction;
if (nextPanel >= 0 && nextPanel < panelsCount) {
this.setState({ activePanel: nextPanel });
}
}
}
這時候就可以實現滑動卡片功能了
示例圖:

使用react-native-video音樂插入
1.導入需要的組件和音樂
import Video from "react-native-video";
import url1 from "../static/video/徐小明-漁歌.mp3";
2.使用組件
<Video
source={require('./background.mp4')} // 視頻的URL地址,或者本地地址
//source={require('./music.mp3')} // 可以播放音頻
//source={{uri:'http://......'}}
ref='player'
rate={this.state.isPlay?1:0} // 控制暫停/播放,0 代表暫停paused, 1代表播放normal.
volume={1.0}
// 聲音的放聲音的放大倍數大倍數,0 為靜音 ,1 為正常音量 ,更大的數字表示放大的倍數
muted={false} // true代表靜音,默認為false.
paused={false} // true代表暫停,默認為false
resizeMode="contain" // 視頻的自適應伸縮鋪放行為,contain、stretch、cover
repeat={false} // 是否重復播放
playInBackground={false} // 當app轉到后台運行的時候,播放是否暫停
playWhenInactive={false} // [iOS] Video continues to play when control or notification center are shown. 僅適用於IOS
onLoadStart={this.loadStart} // 當視頻開始加載時的回調函數
onLoad={this.setDuration} // 當視頻加載完畢時的回調函數
onProgress={this.setTime} // 進度控制,每250ms調用一次,以獲取視頻播放的進度
onEnd={this.onEnd} // 當視頻播放完畢后的回調函數
onError={this.videoError} // 當視頻不能加載,或出錯后的回調函數
style={styles.backgroundVideo}
/>
3.我設置組件的一些屬性
<Video
ref={video => (this.player = video)}
source={SONGS[this.state.activePanel].url}
ref="video"
paused={this.state.paused}
onLoad={data => this.setDuration(data)}
volume={1.0}
paused={false}
onEnd={() => this.goToPanel(1)}
playInBackground={true}
onProgress={e => this.setTime(e)}
playWhenInactive={true}
/>
4.用到的函數
constructor() {
super();
this.state = {
activePanel: 0, //當前active的面板
activeSong: SONGS[0], //正在播放的歌
currentTime: 0.0, //當前播放的時間
paused: 1.0, //播放
sliderValue: 0, //進度條的進度
duration: 0.0 //總時長
};
}
//格式化音樂播放的時間為0:00
formatMediaTime(duration) {
let min = Math.floor(duration / 60);
let second = duration - min * 60;
min = min >= 10 ? min : "0" + min;
second = second >= 10 ? second : "0" + second;
return min + ":" + second;
}
//設置進度條和播放時間的變化
setTime(data) {
let sliderValue = parseInt(this.state.currentTime);
this.setState({
slideValue: sliderValue,
currentTime: data.currentTime
});
}
//設置總時長
setDuration(duration) {
this.setState({ duration: duration.duration });
}
進度條的配置
<Slider
style={styles.slider}
value={this.state.slideValue}
maximumValue={this.state.duration}
step={1}
onValueChange={value => this.setState({ currentTime: value })}
/>
