React Native Expo:无法从空播放源加载AV资产

mm5n2pyu  于 2022-12-19  发布在  React
关注(0)|答案(1)|浏览(105)

嗨,我尝试使用Expo-AV,但不断收到[Unhandled promise rejection: Error: Cannot load an AV asset from a null playback source]警告
当声音播放函数第一次被调用时,它显示此警告,并且不播放,但在它之后,当我再次调用该函数时,它播放时没有警告。

const [sound, setSound] = useState();
const [isPlaying, setIsPlaying] = useState(false);

async function playSound() {
    console.log("Loading Sound");
    const { sound } = await Audio.Sound.createAsync(
      { uri },
      { shouldPlay: true }
    );
    setSound(sound);
    console.log("Playing Sound");
    setIsPlaying(true);
    await sound.playAsync();
    sound._onPlaybackStatusUpdate = (status) => {
      if (status.didJustFinish) {
        setIsPlaying(false);
        console.log("Finished");
      }
    };
  }
<TouchableOpacity onPress={playSound()}> 
  <Text>Play</Text>
</TouchableOpacity>

有没有反正发挥后,加载正确.

nkoocmlb

nkoocmlb1#

这对我很有效。试试看。在这里,我正在播放一个音频,它是由用户从Expo的DocumentPicker库中选择的。

const prepareToPlay = async (audioPath) => {
    const sound = new Audio.Sound();

    try {
        await sound.loadAsync({
            uri: audioPath.file,
            shouldPlay: true,
        });
        await sound.playAsync();
        // Your sound is playing!

        // Dont forget to unload the sound from memory
        // when you are done using the Sound object
        // await sound.unloadAsync();
    } catch (error) {
        // An error occurred!
        console.error('AUDIO PLAY: ', error);
    }
};

const audioToBePlayed =  {
    "file": "file:///Users/crosbyroads/Library/Developer/CoreSimulator/Devices/54AE93CW-1667-491F-A9C7-B457N8BC1207/data/Containers/Data/Application/5C442DA1-4EEA-4F24-93C2-38131484EE9E/Library/Caches/ExponentExperienceData/%crosbyroads%252FPlaySounds/DocumentPicker/EE3ECE8E-DFDC-46C9-8B68-4A8E0A8BB808.wav",
    "name": "assets_audio_OS_SB_160_Dm_Cream_Stack_1.wav",
    "size": 3180496,
    "type": "audio/wav",
  }

...

<Button onPress={() => prepareToPlay(audioToBePlayed)} title="Play"></Button>

/** Subscribe to my channel @ https://youtube.com/crosbyroads */

相关问题