PlayerControls.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useState, useContext } from 'react';
  2. import {
  3. View,
  4. StyleSheet,
  5. Text,
  6. Dimensions,
  7. TouchableWithoutFeedback,
  8. Platform
  9. } from 'react-native';
  10. import { Icon, Slider } from 'react-native-elements';
  11. import C from 'rn-class';
  12. import {PlayerStateContext, PlayerDispatchContext, playerCommon} from './PlayerReducer';
  13. const { width } = Dimensions.get('window');
  14. export const PLACEHOLDER_WIDTH = width / 4;
  15. export default PlayerControls = props => {
  16. const { Pages, closePlayer, currentPlayerValue, setCurrentPlayerValue} = props;
  17. const [curPlayerValue, setCurPlayerValue] = useState(0);
  18. const [isPlay, setPlay] = useState(false);
  19. const playerDispatch = useContext(PlayerDispatchContext);
  20. const playerState = useContext(PlayerStateContext);
  21. const {paused, player, duration, currentTime} = playerState;
  22. const { secondsTohhmmss } = playerCommon;
  23. const {playerData} = Pages;
  24. const onSetPlay = () => {
  25. //setPlay(!isPlay);
  26. playerDispatch({type:'paused', setPaused:!paused});
  27. };
  28. const getThumbTopFromStyleHeight = (styleHeight=40, trackHeight=4) => {
  29. const thumbTop = (styleHeight / 2) + (trackHeight / 2) +
  30. (Platform.OS==='android'&&(styleHeight<40||trackHeight<4)?0.5:0);
  31. return {
  32. sh: {height:styleHeight},
  33. th: {height:trackHeight},
  34. tt: {top:thumbTop}
  35. }
  36. }
  37. const sliderLayout = getThumbTopFromStyleHeight(20,2);
  38. return (
  39. <>
  40. <View style={styles.container}>
  41. <View style={styles.placeholder} />
  42. <C.View cls="flx2">
  43. <C.View cls="flx-row jc-sb">
  44. <Text numerOfLine={1}>{playerData.title.replace(/\r\n|\n|\r/gm,' ')}</Text>
  45. <Text>{secondsTohhmmss(duration)}</Text>
  46. </C.View>
  47. <Slider
  48. style={sliderLayout.sh}
  49. trackStyle={sliderLayout.th}
  50. thumbTintColor="#333"
  51. thumbStyle={{width:10, height:5, ...sliderLayout.tt}}
  52. maximumValue={duration}
  53. value={currentTime}
  54. onValueChange={value => playerDispatch({type: 'seek', seekValue: Math.floor(value)})}
  55. />
  56. </C.View>
  57. <C.View cls="flx0.5 jc-sa ai-sa flx-row">
  58. { paused===false
  59. ?<C.EL.Icon {...C.n2cls("size8")} type="ionicon" name="md-pause" onPress={onSetPlay} />
  60. :<C.EL.Icon {...C.n2cls("size8")} type="ionicon" name="md-play" onPress={onSetPlay} />
  61. }
  62. </C.View>
  63. <C.EL.Icon ccls="mr2" type="ionicon" name="md-close" onPress={closePlayer} />
  64. </View>
  65. </>
  66. );
  67. };
  68. const styles = StyleSheet.create({
  69. container: {
  70. ...StyleSheet.absoluteFillObject,
  71. flexDirection: 'row',
  72. justifyContent: 'space-around',
  73. alignItems: 'center'
  74. },
  75. placeholder: {
  76. width: PLACEHOLDER_WIDTH
  77. }
  78. });