如何解决Video-React:无法订阅播放器状态,因为播放器未定义
我已经使用 Video-React 实现了视频播放器:
<Player ref={(player) => { this.player = player }}>
<source src="https://media.w3.org/2010/05/sintel/trailer_hd.mp4" />
</Player>
我正在尝试使用 subscribeToStateChange
内的 componentDidMount
订阅玩家状态更改。
但是,this.player
是 undefined
。
componentDidMount() {
// subscribe state change
// player is undefined
this.player.subscribeToStateChange(this.handleStateChange.bind(this));
}
handleStateChange(state,prevState) {
// copy player state to this component's state
this.setState({
player: state,currentTime: state.currentTime
});
}
我添加了一个按钮来尝试在 componentDidMount 之外进行订阅:
onTestButtonClicked() {
// subscribe state change
this.player.subscribeToStateChange(this.handleStateChange.bind(this));
}
它有效。
但是,我需要让它在 componentDidMount
内工作,因为它应该自动完成。
欢迎提出任何建议。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。