5

expo-av用来显示视频。视频已播放,Portrait我正在尝试根据设备方向显示视频。我的代码是:

import { Video } from 'expo-av';
import * as ScreenOrientation from 'expo-screen-orientation';
import NavigationHelper from '../../../../Helpers/NavigationHelper';

export default class VideoScreen extends Component {
  render() {
    const { route } = this.props;
    const { videoUri } = route.params;

    if (!videoUri) {
      NavigationHelper.back();
    }

    return (
      <ScrollView style={styles.container}>
        <Video
          source={{ uri: videoUri }}
          rate={1.0}
          volume={1.0}
          isMuted={false}
          resizeMode={Video.RESIZE_MODE_CONTAIN}
          shouldPlay
          isLooping
          useNativeControls
          style={{ width: 300, height: 300, alignSelf: 'center' }}
          onReadyForDisplay={() => { const naturalSize = ScreenOrientation.Orientation.PORTRAIT ? { orientation: 'portrait' } : { orientation: 'landscape' }; }}
        />
      </ScrollView>
    );
  }
}

我已经看到这onReadyForDisplay是在视频准备好显示时调用的函数。该函数被传递一个具有以下键值对 naturalSize的字典: :具有以下键值对的字典: orientation:描述视频数据的自然方向的字符串,要么portrait要么landscape。我习惯于expo-screen-orientation获取设备方向。

如何根据设备方向旋转视频?

4

1 回答 1

0

你可以试试

import { Video, VideoFullscreenUpdateEvent } from 'expo-av';
import * as ScreenOrientation from 'expo-screen-orientation';

    
  export const onFullscreenUpdate = async ({
      fullscreenUpdate,
    }: VideoFullscreenUpdateEvent) => {
      if (fullscreenUpdate === Video.FULLSCREEN_UPDATE_PLAYER_DID_PRESENT) {
        await ScreenOrientation.unlockAsync();
      } else if (fullscreenUpdate === Video.FULLSCREEN_UPDATE_PLAYER_WILL_DISMISS) {
        await ScreenOrientation.lockAsync(
          ScreenOrientation.OrientationLock.PORTRAIT,
        );
      }
    };


    <Video
       source={{
           uri: videoUrl,
        }}
           onFullscreenUpdate={onFullscreenUpdate}
          ...
          

于 2021-11-10T03:08:12.367 回答