我试图在Expo-AV上播放多个声音文件,但是当一个声音播放时,另一个声音停止。我无法同时播放多个声音。
"我所尝试的"
我尝试了一个类方法来创建一个不同的示例,但每次都不起作用。所以我恢复到功能组件,并同时调用playSound()
来播放,但它不起作用
以下是我的代码:
import React, { useEffect, useState } from "react";
import { Text, View} from "react-native";
import {Audio} from 'expo-av'
const MusicModal = (props) => {
const [sound, setSound] = React.useState(new Audio.Sound());
const [isPlaying, setIsPlaying] = React.useState(false);
useEffect(()=>{
handleAudioConfig()
},[])
async function playSound(song) {
console.log('Loading Sound');
const { sound } = await Audio.Sound.createAsync(song);
setSound(sound);
sound.setIsLoopingAsync(true)
console.log('Playing Sound');
await sound.playAsync().then(()=>{
setIsPlaying(true)
});
}
useEffect(() => {
return sound
? () => {
console.log('Unloading Sound');
sound.unloadAsync();
}
: undefined;
}, [sound]);
const handleAudioConfig = async() =>{
try {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
playsInSilentModeIOS: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
shouldDuckAndroid: true,
staysActiveInBackground: true,
playThroughEarpieceAndroid: false,
})
playSound(require('soundFilePath'))
playSound(require('soundFilePath'))
} catch (e) {
console.log(e)
}
}
return (
<View>
<Text>Play Sound</Text>
</View>
);
};
export default MusicModal;
1条答案
按热度按时间jckbn6z71#
这是我的代码。像这样一个接一个地调用声音:
都包在里面: