简体   繁体   English

如何在 expo-av Video 上使用 playFromPositionAsync? 反应原生

[英]How to use playFromPositionAsync on expo-av Video? ReactNative

I am using the Video Expo component and noticed that there is a prop playFromPositionAsync .我正在使用Video Expo组件并注意到有一个道具playFromPositionAsync

I saw this on Video.d.ts:我在 Video.d.ts 上看到了这个:

export default class Video extends React.Component<VideoProps, VideoState> implements Playback {
  ...
  playFromPositionAsync: (positionMillis: number, tolerances?: {
        toleranceMillisBefore?: number;
        toleranceMillisAfter?: number;
  }) => Promise<AVPlaybackStatus>;
}

I have this on my code:我的代码上有这个:

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}
      />
)

See this line abive: shouldPlay={currentIndex === idx}看到这一行: shouldPlay={currentIndex === idx}

I want to do similar with playFromPositionAsync我想用playFromPositionAsync做类似的事情

<Video playFromPositionAsync={currentIndex === idx && playFromPositionAsync(0)}

Well, that code above doesn't work.好吧,上面的代码不起作用。

I need to use that prop/function: playFromPositionAsync when currentIndex === idx , so how can I use it?我需要在currentIndex === idx时使用该道具/功能: playFromPositionAsync ,那么我该如何使用它呢?

I saw an example like this: https://github.com/expo/playlist-example/blob/51718bc8bf398bdccda46748e777c294cd40db99/App.js#L404 but the example shows a class based component, and I am using functional/stateless components.我看到了一个这样的例子: https://github.com/expo/playlist-example/blob/51718bc8bf398bdccda46748e777c294cd40db99/App.js#L404但这个例子显示了一个 ZA2F2ED4F8EBC2CBB4C21A29 使用基于功能的组件和无状态组件。

Any ideas?有任何想法吗?

According to the documentation , you have to use ref on a Video instance.根据文档,您必须在Video实例上使用ref And then access playFromPositionAsync by this 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM