如何解决“在 iOS 上使用 expo 播放视频但没有声音”的问题

Phạ*_*Tài 3 video ios react-native expo react-native-video

我使用世博会的视频组件。我可以播放视频,但在 iOS 中没有声音。在安卓上没问题。我该如何解决它。

<Video style={{ width: 340, 
                                height: 220,
                                borderRadius: 10, 
                                overflow: 'hidden' }}
                                posterSource={require('../../assets/loading2.gif')}
                                usePoster={true}
                                rate={1.0}
                                isMuted={false}
                                useNativeControls = {true}
                                volume={1.0}
                                playsInSilentLockedModeIOS ={ true }
                                resizeMode='cover' 
                                shouldPlay={true} 
                                source={{uri : url}} />
Run Code Online (Sandbox Code Playgroud)

Ahm*_*mad 8

音频无法正常工作,因为我将手机设为静音。要更改此默认行为,请设置playsInSilentLockedModeIOS={ true }

(我知道OP已经在他们的代码中包含了这个,但我回答只是为了以防万一这对某人有帮助)。


man*_*htr 6

这对我有用:

  1. ref创建对该组件的引用 ( ) <Video />
  2. 使用状态来监听视频开始播放时的情况。
  3. 当出现这种情况时,请致电await Audio.setAudioModeAsync({ playsInSilentModeIOS: true });.
  4. 之后立即通过调用重新播放视频ref.current.playAsync();

总之,我的组件如下所示:

import React, { useState, useEffect, useRef } from 'react';
import { Audio, Video as OriginalVideo } from 'expo-av';

const triggerAudio = async (ref) => {
  await Audio.setAudioModeAsync({ playsInSilentModeIOS: true });
  ref.current.playAsync();
};

const Video = ({ ...props }) => {
  const ref = useRef(null);
  const [status, setStatus] = useState({});

  useEffect(() => {
    if (status.isPlaying) triggerAudio(ref);
  }, [ref, status.isPlaying]);

  return (
    <OriginalVideo
      ref={ref}
      onPlaybackStatusUpdate={(status) => setStatus(status)}
      useNativeControls
      {...props}
    />
  );
};

Run Code Online (Sandbox Code Playgroud)

使用 Expo SDK v45 和 iOS 16 进行测试。

希望能帮助到你!