我正在使用Video Expo组件,并注意到有一个合适的playFromPositionAsync。
我在Video.d.ts上看到了这个:
export default class Video extends React.Component<VideoProps, VideoState> implements Playback {
...
playFromPositionAsync: (positionMillis: number, tolerances?: {
toleranceMillisBefore?: number;
toleranceMillisAfter?: number;
}) => Promise<AVPlaybackStatus>;
}我的代码中有这样的代码:
import { Video } from 'expo-av';
...
return data.feed.map((item: DataType, idx: number) => (
<Video
key={item.id}
useNativeControls={false}
isMuted={currentIndex !== idx}
source={{ uri: item.video_url }}
shouldPlay={currentIndex === idx}
/>
)请看下面这一行:shouldPlay={currentIndex === idx}
我想用playFromPositionAsync做类似的事情
<Video playFromPositionAsync={currentIndex === idx && playFromPositionAsync(0)}好吧,上面的代码不起作用。
我需要在currentIndex === idx时使用这个属性/函数:playFromPositionAsync,那么我该如何使用它呢?
我看到了一个这样的例子:https://github.com/expo/playlist-example/blob/51718bc8bf398bdccda46748e777c294cd40db99/App.js#L404,但是这个例子显示了一个基于类的组件,而我使用的是函数式/无状态组件。
有什么想法吗?
发布于 2020-06-29 15:31:10
根据documentation,您必须在Video实例上使用ref。然后通过这个ref访问playFromPositionAsync
...
const _handleVideoRef = (component, index) => {
const playbackObject = component;
if (playbackObject && index === currentIndex) {
playbackObject.playFromPositionAsync(0)
}
...
}
...
render() {
return data.feed.map((item: DataType, idx: number) => (
<Video
key={item.id}
ref={(component) => _handleVideoRef(component, idx)} // add passing ref here
useNativeControls={false}
isMuted={currentIndex !== idx}
source={{ uri: item.video_url }}
shouldPlay={currentIndex === idx}
/>
)
...https://stackoverflow.com/questions/62631794
复制相似问题