【问题标题】:Cannot read property 'seek' of undefined无法读取未定义的属性“寻求”
【发布时间】:2019-03-26 05:52:56
【问题描述】:

我正在尝试使用 react-native-video 组件,如下所示:

export default class VideoWrapper extends Component<Props> {

  render() {
    return (
        <Video
          source={require('../../assets/test_sound.mp3')}
          ref={player => {
            this.player = player;
          }}
          muted={false}
          repeat={false}
          resizeMode={"cover"}
          volume={1.0}
          rate={1.0}
          ignoreSilentSwitch={"obey"}
          onProgress={this.onProgress}
          onSeek={this.onSeek}
          onEnd={this.onEnd}
          onError={this.onError} 
        />
    );
  }
  onEnd() {
    this.player.seek(0);
  }
  ...

结果:

ExceptionsManager.js:74 无法读取未定义的属性 'seek'

如果我将 onEnd 方法更改为以下内容,它将起作用:

onEnd={ () => this.player.seek(0) }

我不想使用第二种方法,因为它弄乱了我的 xml 代码。我该如何解决第一种方法?

【问题讨论】:

  • 你试过onEnd() { if (this.player) this.player.seek(0); }吗?
  • 是的。 this.player 永远不会被设置,所以搜索永远不会发生。
  • 你在构造函数中绑定了onEnd 方法??还是您尝试将其转换为箭头函数以获取 this 上下文?

标签: react-native react-native-video


【解决方案1】:

试试这个:

onEnd = () => {
    this.player.seek(0);
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2021-12-10
    • 1970-01-01
    • 2020-04-06
    • 2017-04-06
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多