【问题标题】:Prevent the reading of video react native防止阅读视频 react native
【发布时间】:2021-12-23 10:22:45
【问题描述】:

我目前正在开发一个允许用户观看体育视频的应用程序,我想实现以下功能: 用户会看到一个视频列表,但如果他首先查看第一个视频,则只能看到下一个视频。目前,我只是在应该被阻止的视频的缩略图上添加了一个锁,但用户仍然可以点击一边播放视频。我查看了 react-native-video 包的所有道具,但没有看到任何适合我需要的道具。在 你有想法吗?

这里也是代码示例:

<View style={styles.videoRow}>
            <View>
              <Video
                style={styles.image}
                source={{uri: 'https://firebasestorage.googleapis.com/v0/b/roundpower-88ef9.appspot.com/o/BootyAbsPower%2FTuto%2013.mp4?alt=media&token=da011245-fce2-4796-a78b-3abc518c73ef'}}
                useNativeControls
                resizeMode="contain"
                isLooping
                onPlaybackStatusUpdate={(playbackStatus) => onPlaybackStatusUpdate3(playbackStatus)}
              />
              {!debloque3 ? <View>
                <FontAwesome5 name="lock" size={40} color="white" style={styles.icon}/>
              </View> : <Text>''</Text>}

            </View>
            <View>
              <Video
                style={styles.image}
                source={{uri: 'https://firebasestorage.googleapis.com/v0/b/roundpower-88ef9.appspot.com/o/BootyAbsPower%2FTuto%2014.mp4?alt=media&token=cd3d12be-05fd-4fc4-91d9-cd518faf14ce'}}
                useNativeControls
                resizeMode="contain"
                isLooping
                onPlaybackStatusUpdate={(playbackStatus) => onPlaybackStatusUpdate4(playbackStatus)}
              />
              {!debloque4 ? <View>
                <FontAwesome5 name="lock" size={40} color="white" style={styles.icon}/>
              </View> : <Text>''</Text>}
            </View>
          </View>

【问题讨论】:

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


    【解决方案1】:

    是的,一个非常简单的方法就是阻止你不想让用户点击的东西接收任何pointerEvents(即触摸事件)。

    这样做的一个非常简单的快速和肮脏的方式:

    import * as React from 'react';
    import {TouchableOpacity, StyleSheet, View} from 'react-native';
    
    const onPress = () => console.error("I don't want this to happen.");
    
    export default () => (
      <View style={[StyleSheet.absoluteFill, styles.center]}>
        {/* red box in the middle */}
        <View style={{width: 50, height: 50, backgroundColor: 'red'}}>
          <TouchableOpacity
            onPress={onPress}
            style={StyleSheet.absoluteFill}
          />
          {/* Obscure the TouchableOpacity with a View which completely covers it */}
          {shouldPreventTouches && <View style={StyleSheet.absoluteFill} />}
        </View>
      </View>
    );
    

    或者,您也可以将&lt;Video /&gt; 组件包装在&lt;View /&gt;useState 中,以在pointerEvents="none"pointerEvents="auto" 之间切换。两者都具有相同的防止触摸信息传递给孩子的效果:

    import * as React from 'react';
    import {TouchableOpacity, StyleSheet, View} from 'react-native';
    
    const onPress = () => console.error("I don't want this to happen.");
    
    export default () => (
      <View style={[StyleSheet.absoluteFill, styles.center]}>
        {/* red box in the middle */}
        <View
          pointerEvents={shouldPreventTouches ? 'none' : 'auto'}
          style={{width: 50, height: 50, backgroundColor: 'red'}}>
          <TouchableOpacity
            onPress={onPress}
            style={StyleSheet.absoluteFill}
          />
        </View>
      </View>
    );
    

    看你喜欢什么。

    【讨论】:

      【解决方案2】:

      您可以重复使用在视频元素上显示或不显示锁定的逻辑,并仅在视频解锁时呈现视频。

          <View>
          {
              debloque ? <Video/> : <FontAwesome5 name="lock" size={40} color="white" style={styles.icon} />
          }
          </View>
      

      或者,根据the docs,您可以将视频源设置为 null 直到它被解锁,以初始化播放器但阻止播放任何内容。

      【讨论】:

      • 非常感谢,但问题是我希望用户看到视频的缩略图。但是如果视频被锁定,我能做的就是用视频缩略图替换视频!非常感谢!
      猜你喜欢
      • 2016-07-11
      • 2017-11-10
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2015-09-01
      • 1970-01-01
      • 2018-04-16
      相关资源
      最近更新 更多