android - React Native Video,如何动态更改源?
问题描述
我正在尝试在 React-Native 中播放视频。问题是我可以使用相对路径(“../videos/test.mp4”)来实现这一点,但是当我使用像(this.state.path)这样的变量时,我会收到以下错误下方留言。有人可以帮我吗?
错误 点击这里查看错误
主班
export default class level1 extends React.Component {
constructor() {
super();
this.state = {
path: script[0].path,
repeat: script[0].repeat,
finished: false,
scriptIterator: 0
}
this.videoFinished = this.videoFinished.bind(this);
}
videoFinished() {
var i = this.state.scriptIterator + 1;
if (this.state.repeat) {
// clicklistener
console.warn("clicklistener");
}
this.setState({
finished: false,
scriptIterator: i,
path: script[i].path,
repeat: script[i].repeat
})
}
render() {
return (
<View>
<VideoClass
path={this.state.path}
repeat={this.state.repeat}
onEnd={this.videoFinished}
/>
</View>
)
}
}
const script =
[
{ path: "../videos/APB.mp4", repeat: false },
{ path: "../videos/test.mp4", repeat: true }
]
视频类
export default class VideoClass extends React.Component {
constructor(props) {
super(props);
this.state = {
path: props.path,
repeat: props.repeat
}
}
render() {
return (
<Video
source={require(this.state.path)}
style={styles.backgroundVideo}
muted={true}
repeat={this.state.repeat}
resizeMode={"cover"}
rate={1.0}
ignoreSilentSwitch={"obey"}
onEnd={() => this.props.onEnd()}
/>
);
}
}
解决方案
首先,在您的 VideoClass 组件中,您应该传递this.props.path
给source
属性和this.props.repeat
属性repeat
,因为您的状态是在您的 level1 组件中控制的
<Video
source={this.props.path}
style={styles.backgroundVideo}
muted={true}
repeat={this.props.repeat}
resizeMode={"cover"}
rate={1.0}
ignoreSilentSwitch={"obey"}
onEnd={() => this.props.onEnd()}
/>
然后在您的 level1 组件中,您应该将script
const 更改为:
const script =
[
{ path: require("../videos/APB.mp4"), repeat: false },
{ path: require("../videos/test.mp4"), repeat: true }
]
推荐阅读
- javascript - 如何使用侧边栏表单的输入并将其存储为 Google Apps 脚本中的全局变量?
- sql - 如何连接 3 个表,其中一个或多个给定 ID 可能存在于一个或多个表中,每个 ID 只有一行?
- ios - 列表可折叠部分的 SwiftUI 更改图标
- wordpress - 用户角色在 WordPress 中消失
- apache-spark - 我们如何在 Azure 上的 Kubernetes 上为 Spark 提供不同的驱动程序和执行程序?
- python - voice_channel.play() 不播放音频
- kentico - Kentico Xperience 活动 - 如何添加新活动
- algorithm - 哈希函数 Sha256 无法追踪?
- python - 更改100%堆叠条形图中的颜色plotly python
- altair - Interactive() 不能与 alt.layer 一起使用?