react-native-video/examples/basic/index.android.js

231 lines
5.8 KiB
JavaScript
Raw Normal View History

2015-12-06 23:25:18 -07:00
'use strict';
import React, {
2016-04-21 22:01:44 -06:00
Component
} from 'react';
import {
2015-12-06 23:25:18 -07:00
AppRegistry,
StyleSheet,
Text,
TouchableOpacity,
2016-02-10 04:39:54 -07:00
View,
} from 'react-native';
import Video from 'react-native-video';
class VideoPlayer extends Component {
state = {
rate: 1,
volume: 1,
muted: false,
resizeMode: 'contain',
duration: 0.0,
currentTime: 0.0,
2017-01-11 05:51:45 -07:00
paused: true,
2016-02-10 04:39:54 -07:00
};
2015-12-06 23:25:18 -07:00
2017-01-11 05:51:45 -07:00
video: Video;
2015-12-06 23:25:18 -07:00
2017-01-11 05:51:45 -07:00
onLoad = (data) => {
this.setState({ duration: data.duration });
};
onProgress = (data) => {
this.setState({ currentTime: data.currentTime });
};
onEnd = () => {
this.setState({ paused: true })
this.video.seek(0)
};
onAudioBecomingNoisy = () => {
this.setState({ paused: true })
};
onAudioFocusChanged = (event: { hasAudioFocus: boolean }) => {
this.setState({ paused: !event.hasAudioFocus })
};
2015-12-06 23:25:18 -07:00
getCurrentTimePercentage() {
if (this.state.currentTime > 0) {
return parseFloat(this.state.currentTime) / parseFloat(this.state.duration);
}
2017-01-11 05:51:45 -07:00
return 0;
};
2015-12-06 23:25:18 -07:00
renderRateControl(rate) {
2017-01-11 05:51:45 -07:00
const isSelected = (this.state.rate === rate);
2015-12-06 23:25:18 -07:00
return (
2017-01-11 05:51:45 -07:00
<TouchableOpacity onPress={() => { this.setState({ rate }) }}>
<Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>
2015-12-06 23:25:18 -07:00
{rate}x
</Text>
</TouchableOpacity>
2017-01-11 05:51:45 -07:00
);
2016-02-10 04:39:54 -07:00
}
2015-12-06 23:25:18 -07:00
renderResizeModeControl(resizeMode) {
2017-01-11 05:51:45 -07:00
const isSelected = (this.state.resizeMode === resizeMode);
2015-12-06 23:25:18 -07:00
return (
2017-01-11 05:51:45 -07:00
<TouchableOpacity onPress={() => { this.setState({ resizeMode }) }}>
<Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>
2015-12-06 23:25:18 -07:00
{resizeMode}
</Text>
</TouchableOpacity>
)
2016-02-10 04:39:54 -07:00
}
2015-12-06 23:25:18 -07:00
renderVolumeControl(volume) {
2017-01-11 05:51:45 -07:00
const isSelected = (this.state.volume === volume);
2015-12-06 23:25:18 -07:00
return (
2017-01-11 05:51:45 -07:00
<TouchableOpacity onPress={() => { this.setState({ volume }) }}>
<Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>
2015-12-06 23:25:18 -07:00
{volume * 100}%
</Text>
</TouchableOpacity>
)
2016-02-10 04:39:54 -07:00
}
2015-12-06 23:25:18 -07:00
render() {
2016-02-10 04:39:54 -07:00
const flexCompleted = this.getCurrentTimePercentage() * 100;
const flexRemaining = (1 - this.getCurrentTimePercentage()) * 100;
2015-12-06 23:25:18 -07:00
return (
<View style={styles.container}>
2017-01-11 05:51:45 -07:00
<TouchableOpacity
style={styles.fullScreen}
onPress={() => this.setState({ paused: !this.state.paused })}
>
<Video
ref={(ref: Video) => { this.video = ref }}
/* For ExoPlayer */
/* source={{ uri: 'http://www.youtube.com/api/manifest/dash/id/bf5bb2419360daf1/source/youtube?as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,source,id,as&ip=0.0.0.0&ipbits=0&expire=19000000000&signature=51AF5F39AB0CEC3E5497CD9C900EBFEAECCCB5C7.8506521BFC350652163895D4C26DEE124209AA9E&key=ik0', type: 'mpd' }} */
source={require('./broadchurch.mp4')}
2017-01-11 05:51:45 -07:00
style={styles.fullScreen}
rate={this.state.rate}
paused={this.state.paused}
volume={this.state.volume}
muted={this.state.muted}
resizeMode={this.state.resizeMode}
onLoad={this.onLoad}
onProgress={this.onProgress}
onEnd={this.onEnd}
onAudioBecomingNoisy={this.onAudioBecomingNoisy}
onAudioFocusChanged={this.onAudioFocusChanged}
repeat={false}
/>
2015-12-06 23:25:18 -07:00
</TouchableOpacity>
<View style={styles.controls}>
<View style={styles.generalControls}>
<View style={styles.rateControl}>
{this.renderRateControl(0.25)}
{this.renderRateControl(0.5)}
{this.renderRateControl(1.0)}
{this.renderRateControl(1.5)}
{this.renderRateControl(2.0)}
</View>
<View style={styles.volumeControl}>
{this.renderVolumeControl(0.5)}
{this.renderVolumeControl(1)}
{this.renderVolumeControl(1.5)}
</View>
<View style={styles.resizeModeControl}>
{this.renderResizeModeControl('cover')}
{this.renderResizeModeControl('contain')}
{this.renderResizeModeControl('stretch')}
</View>
</View>
<View style={styles.trackingControls}>
<View style={styles.progress}>
2017-01-11 05:51:45 -07:00
<View style={[styles.innerProgressCompleted, { flex: flexCompleted }]} />
<View style={[styles.innerProgressRemaining, { flex: flexRemaining }]} />
2015-12-06 23:25:18 -07:00
</View>
</View>
</View>
</View>
);
}
2016-02-10 04:39:54 -07:00
}
2015-12-06 23:25:18 -07:00
2016-02-10 04:39:54 -07:00
const styles = StyleSheet.create({
2015-12-06 23:25:18 -07:00
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'black',
},
fullScreen: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
controls: {
2017-01-11 05:51:45 -07:00
backgroundColor: 'transparent',
2015-12-06 23:25:18 -07:00
borderRadius: 5,
position: 'absolute',
bottom: 20,
left: 20,
right: 20,
},
progress: {
flex: 1,
flexDirection: 'row',
borderRadius: 3,
overflow: 'hidden',
},
innerProgressCompleted: {
height: 20,
backgroundColor: '#cccccc',
},
innerProgressRemaining: {
height: 20,
backgroundColor: '#2C2C2C',
},
generalControls: {
flex: 1,
flexDirection: 'row',
borderRadius: 4,
overflow: 'hidden',
paddingBottom: 10,
},
rateControl: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
},
volumeControl: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
},
resizeModeControl: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
controlOption: {
alignSelf: 'center',
fontSize: 11,
2017-01-11 05:51:45 -07:00
color: 'white',
2015-12-06 23:25:18 -07:00
paddingLeft: 2,
paddingRight: 2,
lineHeight: 12,
},
});
AppRegistry.registerComponent('VideoPlayer', () => VideoPlayer);