[英]How to use playFromPositionAsync on expo-av Video? ReactNative
我正在使用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但這個例子顯示了一個 ZA2F2ED4F8EBC2CBB4C21A29 使用基於功能的組件和無狀態組件。
有任何想法嗎?
根據文檔,您必須在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}
/>
)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.