嗨,我试图使用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>
加载正确后还有什么要玩的吗。
这对我有用。试试吧在这里,我正在播放一个音频,该音频是用户用DocumentPicker库从Expo中选择的。
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>