Проблемы с воспроизведением видео React и обложкой изображений - PullRequest
0 голосов
/ 05 февраля 2019

Я пытаюсь воссоздать Snapchat Snapchat Discover, я применил приведенный ниже код, и когда я нажимаю на изображение обложки, ничего не происходит.Если я вручную установлю для своего состояния значение true, flatlist отобразит видео, и я смогу воспроизвести любое из видео.Я хотел бы нажать на изображение и переключиться на видео.

 class Games extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      show: false
    };
  }

  playVideo() {
    this.setState({ show: true });
  }

  render() {
    const renderVideo = ({ item, index }) => {
      return (
        <TouchableOpacity onPress={this.playVideo()}>
             {this.state.show ?
            <VideoPlayer
              videoProps={{
                shouldPlay: true,
                resizeMode: Video.RESIZE_MODE_CONTAIN,
                source: {
                  uri: 'https://gcs-vimeo.akamaized.net/exp=1549330881~acl=%2A%2F671569878.mp4%2A~hmac=17bb2f7f2be7c20848448cfc810096c82cf7e7715b7fa43566c4a899912fa42b/vimeo-prod-skyfire-std-us/01/4838/7/199191069/671569878.mp4',
                },
              }}
              isPortrait
              playFromPositionMillis={0}
            />
:
  <View
          style={[
            { width: Dimensions.get('window').width / 2.2 },
            { height: 250,
              margin: 8
          }]}
        >
            <Image
              square
              source={{ uri: 'https://pixabay.com/get/ea34b90a29f3013ed1534705fb094797e771e0dd11b50c4090f4c87aa5e9bcbfdd/training-3185170_1920.jpg' }}
              key={index}
              style={{
                flex: 1,
                height: undefined,
                width: undefined,
                borderRadius: 10,
                borderWidth: 0.5,
                borderColor: '#dddddd'
              }}
            />
  </View>}
        </TouchableOpacity>

      );
    };


    if (this.props.game.isLoading) {
      return (
        <Loading />
      );
    }
    else if (this.props.game.errMess) {
      return (
        <View>
          <Text>{this.props.game.errMess}</Text>
        </View>
      );
    }
    else {
      return (
          <FlatList
            data={this.props.events.events}
            renderItem={renderVideo}
            keyExtractor={item => item.id.toString()}
            showsVerticalScrollIndicator={false}
            contentContainerStyle={styles.container}
            numColumns={2}
          />
      );
    }
  }
}

Я новичок в реакции на родную систему, поэтому не стесняйтесь вызывать меня по любой ошибке, которую вы можете заметить в моем коде.

Ответы [ 2 ]

0 голосов
/ 06 февраля 2019
    constructor(props) {
    super(props);
    this.state = {
      isModalVisible: false,
      name: ''
    };
  }

  render() {
    const renderVideoPic = ({ item, index }) => {
      return (
        <TouchableOpacity onPress={() => this.setState({ isModalVisible: !this.state.isModalVisible, name: item.first_name })}>
          <View
                  style={[
                    { width: Dimensions.get('window').width / 2.2 },
                    { height: 250,
                      margin: 8
                  }]}
                >
                    <Image
                      square
                      source={{ uri: item.image }}
                      key={index}
                      style={{
                        flex: 1,
                        height: undefined,
                        width: undefined,
                        borderRadius: 10,
                        borderWidth: 0.5,
                        borderColor: '#dddddd'
                      }}
                    />
          </View>
        </TouchableOpacity>
      );
    };

    if (this.props.game.isLoading) {
      return (
        <Loading />
      );
    }
    else if (this.props.game.errMess) {
      return (
        <View>
          <Text>{this.props.game.errMess}</Text>
        </View>
      );
    }
    else {
      return (
        <View style={{ flex: 1 }}>
          <FlatList
            data={this.props.events.events}
            renderItem={renderVideoPic}
            keyExtractor={item => item.id.toString()}
            showsVerticalScrollIndicator={false}
            contentContainerStyle={styles.container}
            numColumns={2}
          />
        <TouchableOpacity>
          <Modal
            isVisible={this.state.isModalVisible}
            //animationOut='slideOutDown'
            //onBackdropPress={() => this.setState({ isVisible: false })}
            backdropOpacity={1}
            backdropColor='white'
            style={{
              justifyContent: 'center',
              alignItems: 'center',
              paddingTop: 15
            }}
            onSwipe={() => this.setState({ isModalVisible: false })}
            swipeDirection="down"
            swipeThreshold={200}
          >
            <VideoPlayer
              videoProps={{
                shouldPlay: true,
                resizeMode: Video.RESIZE_MODE_CONTAIN,
                isMuted: false,
                source: {
                  uri: 'http://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
                },
              }}
                isPortrait
                playFromPositionMillis={0}
                showFullscreenButton
                //switchToLandscape={() => ScreenOrientation.allow(ScreenOrientation.Orientation.LANDSCAPE)}
              //switchToPortrait={() => ScreenOrientation.allow(ScreenOrientation.Orientation.PORTRAIT)}
            />
            <Text>{this.state.name}</Text>
          </Modal>
        </TouchableOpacity>
        </View>


      );
    }
  }
}
0 голосов
/ 05 февраля 2019

Что происходит, когда вы консольно регистрируете состояние «show»?

Я считаю, что вам нужно привязать «this», чтобы оно определялось при нажатии на playVideo.

class Games extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      show: false
    };
    this.playVideo = this.playVideo.bind(this); // Bind this to playVideo
  }

  playVideo() {
    this.setState({ show: true });
  }

  render() {
    const renderVideo = ({ item, index }) => {
      return (
        <TouchableOpacity onPress={this.playVideo}> // Here we pass the reference to the function because we want onPress to handle this
             {this.state.show ?
            <VideoPlayer
              videoProps={{
                shouldPlay: true,
                resizeMode: Video.RESIZE_MODE_CONTAIN,
                source: {
                  uri: 'https://gcs-vimeo.akamaized.net/exp=1549330881~acl=%2A%2F671569878.mp4%2A~hmac=17bb2f7f2be7c20848448cfc810096c82cf7e7715b7fa43566c4a899912fa42b/vimeo-prod-skyfire-std-us/01/4838/7/199191069/671569878.mp4',
                },
              }}
              isPortrait
              playFromPositionMillis={0}
            />
:
  <View
          style={[
            { width: Dimensions.get('window').width / 2.2 },
            { height: 250,
              margin: 8
          }]}
        >
            <Image
              square
              source={{ uri: 'https://pixabay.com/get/ea34b90a29f3013ed1534705fb094797e771e0dd11b50c4090f4c87aa5e9bcbfdd/training-3185170_1920.jpg' }}
              key={index}
              style={{
                flex: 1,
                height: undefined,
                width: undefined,
                borderRadius: 10,
                borderWidth: 0.5,
                borderColor: '#dddddd'
              }}
            />
  </View>}
        </TouchableOpacity>

      );
    };


    if (this.props.game.isLoading) {
      return (
        <Loading />
      );
    }
    else if (this.props.game.errMess) {
      return (
        <View>
          <Text>{this.props.game.errMess}</Text>
        </View>
      );
    }
    else {
      return (
          <FlatList
            data={this.props.events.events}
            renderItem={renderVideo}
            keyExtractor={item => item.id.toString()}
            showsVerticalScrollIndicator={false}
            contentContainerStyle={styles.container}
            numColumns={2}
          />
      );
    }
  }
}
...