首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native-video中的OnBuffer属性不起作用

react-native-video中的OnBuffer属性不起作用
EN

Stack Overflow用户
提问于 2020-05-29 15:43:40
回答 4查看 6K关注 0票数 2

我使用react-native-video来播放来自url的视频。我使用onBuffer属性来显示加载器,如果视频是缓冲。但在我的例子中,onBuffer不起作用。如果视频很大,那么在网络缓慢或不稳定的情况下,视频需要时间来播放,所以我想显示加载器,如果视频没有播放,但在缓冲模式。

EN

回答 4

Stack Overflow用户

发布于 2020-09-24 11:07:45

我也遇到了同样的问题,我按照下面的建议在android中解决了这个问题:https://github.com/react-native-community/react-native-video/issues/1920#issuecomment-604545238

据我所知,默认情况下android使用的是android player,而android player没有这些功能。但ExoPlayer有很多特性,如缓冲、自适应比特率等。因此,我们必须显式指定使用ExoPlayer。重要的部分是启用multiDex。

这就是修复的建议,它工作得很好:

如果您是如何启用exoplayer的curois,请按照以下步骤操作:

创建react-native.config.js,内容如下:

代码语言:javascript
复制
module.exports = {
  dependencies: {
    'react-native-video': {
      platforms: {
        android: {
          sourceDir: '../node_modules/react-native-video/android-exoplayer',
        },
      },
    },
  },
};

现在启用遵循此https://developer.android.com/studio/build/multidex的multidex

使用gradlew清理和重建来使其正常工作

票数 3
EN

Stack Overflow用户

发布于 2020-05-29 16:09:27

您可以使用react-native-video的onLoadonEnd属性来显示加载器。

例如

代码语言:javascript
复制
<Video
    poster={/*uri*/}
    posterResizeMode="cover"
    source={/*source*/}
    onLoad={()=>/* set loader to true*/}
    onEnd={()=>/* set loader to false*/}
  />
票数 0
EN

Stack Overflow用户

发布于 2020-05-29 16:51:54

我在我的应用程序中做了所有关于缓冲状态的事情,你可以简单地检查缓冲区的状态,并像这样显示加载器。您必须使用onLoadStart, onBuffer and onLoadEnd的组合,下面是一个示例,说明如何还可以构建自定义加载程序并在缓冲时显示。如果你不想使用我的加载器或者这个动画的东西,移除它并渲染你自己的加载器。

代码语言:javascript
复制
    class VideoPlayer extends React.Component {
       constructor (props) {
       super(props)
       this.state = {
          paused: false,
          buffering: true,
          animated: new Animated.Value(0),
          progress: 0,
          duration: 0
    }
  }
  handleMainButtonTouch = () => {
    if (this.state.progress >= 1) {
      this.player.seek(0)
    }

    this.setState(state => {
      return {
        paused: !state.paused
      }
    })
  };
  handleProgressPress = e => {
    const position = e.nativeEvent.locationX
    const progress = (position / 250) * this.state.duration
    const isPlaying = !this.state.paused

    this.player.seek(progress)
  };

  handleProgress = progress => {
    this.setState({
      progress: progress.currentTime / this.state.duration
    })
  };
  onBuffer = ({isBuffering}) => {
    this.setState({ buffering: isBuffering })
    if (isBuffering) {
      this.triggerBufferAnimation()
    }
    if (this.loopingAnimation && isBuffering) {
      this.loopingAnimation.stopAnimation()
    }
  }
  onLoadStart = () => {
    this.triggerBufferAnimation()
  }

  handleLoad = ({duration}) => {
    this.setState({
      duration: duration
    })
  };

  triggerBufferAnimation = () => {
    this.loopingAnimation && this.loopingAnimation.stopAnimation()
    this.loopingAnimation = Animated.loop(
      Animated.timing(this.state.animated, {
        toValue: 1,
        duration: 1500
      })
    ).start()
  };
  render () {
    console.log('video player ', this.props)
    const { isShowingDetails, hideDetails, showDetails, thumbnailUrl, url } = this.props
    const { buffering } = this.state
    const BufferInterpolation = this.state.animated.interpolate({
      inputRange: [0, 1],
      outputRange: ['0deg', '600deg']
    })
    const rotateStyles = { transform: [{
      rotate: BufferInterpolation
    }]
    }
    return (
      <React.Fragment>
        <Video
          ref={ref => {
            this.player = ref;
          }}
          source={{ uri: url }}
          controls
          paused={this.state.paused}
          // poster={thumbnailUrl}
          onBuffer={this.onBuffer}
          onLoadStart={this.onLoadStart}
          onLoad={this.handleLoad}
          key={url}
          onProgress={this.handleProgress}
          style={styles.backgroundVideo}
          useTextureView={false}
          onVideoEnd={() => alert('Video ended')}
          bufferConfig={{
            minBufferMs: 15000,
            maxBufferMs: 50000,
            bufferForPlaybackMs: 2500,
            bufferForPlaybackAfterRebufferMs: 5000
          }}
        />
        <View
          color={Colors.appColor}
          style={styles.videoCover}
        >{
          buffering && <Animated.View style={rotateStyles}>
            <FontAwesome5 name='circle-notch' size={50} color='rgba(255, 255, 255, 0.6)' />
          </Animated.View>
        }</View>
      </React.Fragment>
    )
  }
}

export default VideoPlayer
const styles = StyleSheet.create({
  backgroundVideo: {
    height: undefined,
    width: '100%',
    backgroundColor: 'black',
    aspectRatio: 16 / 9,
    zIndex: 100
    // 2: 0
  },
  videoCover: {
    justifyContent: 'center',
    alignItems: 'center',
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    backgroundColor: 'transparent',
    zIndex: 10
  },
  controls: {
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    height: 20,
    left: 0,
    bottom: 5,
    right: 0,
    position: 'absolute',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-around',
    paddingHorizontal: 10,
    zIndex: 10
  },
  duration: {
    color: '#FFF',
    marginLeft: 15
  }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62081031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档