React本机视频播放和图像覆盖问题

时间:2019-02-05 00:09:56

标签: reactjs react-native video expo video-player

我正在尝试重新创建“发现”,我已应用下面的代码,当我单击封面图像时,没有任何反应。如果我手动将状态设置为true,则平面列表会呈现视频,并且我可以播放任何视频。我想点击图片并切换到视频。

 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 :(得分:0)

当您控制台记录“显示”状态时会发生什么?

我相信您需要绑定“ 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}
          />
      );
    }
  }
}

答案 1 :(得分:0)

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url : '@Url.Action(actionName: "ChildAction", controllerName: "ChildController")',
        dataType: "html",
        async:true,
        success: function (result) { $("#childView").html(result); }
        });

 });