-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
108 lines (108 loc) · 3.65 KB
/
Player.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
/**
* Created by lmy2534290808 on 2017/12/2.
*/
import React, {
Component,
PropTypes
} from 'react';
import {
requireNativeComponent,
View,
Text,
Image,
ActivityIndicator,
} from 'react-native';
class Player extends Component {
constructor(props, context) {
super(props, context);
this.state={
indicatorVisible:false
}
this._onLoading = this._onLoading.bind(this);
this._onPaused = this._onPaused.bind(this);
this._onShutdown = this._onShutdown.bind(this);
this._onPlayerError = this._onPlayerError.bind(this);
this._onPlaying = this._onPlaying.bind(this);
this._onBufferingStart=this._onBufferingStart.bind(this);
this._onBufferingEnd=this._onBufferingEnd.bind(this);
}
_onLoading(event) {
this.props.onLoading && this.props.onLoading(event.nativeEvent);
}
_onPaused(event) {
this.props.onPaused && this.props.onPaused(event.nativeEvent);
}
_onShutdown(event) {
this.props.onShutdown && this.props.onShutdown(event.nativeEvent);
}
_onPlayerError(event) {
this.props.onPlayerError && this.props.onPlayerError(event.nativeEvent);
}
_onPlaying(event) {
this.props.onPlaying && this.props.onPlaying(event.nativeEvent);
}
setNativeProps(nativeProps){
this._root.setNativeProps(nativeProps)
}
_onBufferingStart(event){
this.setState({indicatorVisible:true})
}
_onBufferingEnd(event){
this.setState({indicatorVisible:false})
}
_renderLoadingView(){
let {indicatorVisible}=this.state;
if(indicatorVisible){
return <View style={{position:'absolute',top:0,left:0,right:0,bottom:0,
backgroundColor:'transparent',
justifyContent:"center",alignItems:'center'}}>
<ActivityIndicator size={'large'}/>
<Text style={{color:'black'}}>加载中</Text>
</View>
}else{
return null;
}
}
render() {
let {style,...props}=this.props;
const nativeProps = Object.assign({},props);
Object.assign(nativeProps, {
onLoading: this._onLoading,
onPaused: this._onPaused,
onShutdown: this._onShutdown,
onPlayerError: this._onPlayerError,
onPlaying: this._onPlaying,
onBufferingEnd:this._onBufferingEnd,
onBufferingStart:this._onBufferingStart,
});
return (
<Image source={require('./thumbnail.png')} style={[{justifyContent:'center',alignItems:'center'},style]}>
<RCTPlayerAndroid {...nativeProps}
style={{position:'absolute',top:0,left:0,right:0,bottom:0,backgroundColor:'transparent'}}
ref={ref=>this._root=ref}
/>
{this._renderLoadingView()}
</Image>
)
}
}
Player.propTypes = {
source: PropTypes.shape({ // 是否符合指定格式的物件
uri: PropTypes.string.isRequired,
timeout: PropTypes.number, //Android only
mediaCodec: PropTypes.oneOf([0,1,2]), //Android only
liveStreaming: PropTypes.bool, //Android only
}).isRequired,
paused:PropTypes.bool,
rotation:PropTypes.number,//设置角度
muted:PropTypes.bool, //iOS only
aspectRatio: PropTypes.oneOf([0, 1, 2, 3, 4]),
onLoading: PropTypes.func,
onPaused: PropTypes.func,
onShutdown: PropTypes.func,
onPlayerError: PropTypes.func,
onPlaying: PropTypes.func,
...View.propTypes,
}
const RCTPlayerAndroid = requireNativeComponent('RCTPlayerAndroid', Player);
module.exports = Player;