在手机和模拟器上通过 Expo 应用程序对其进行测试时,一切正常。问题是当我在 Android 设备或 iOS 模拟器上构建应用程序并对其进行测试时,声音不会播放。
import { Audio } from 'expo-av';
const SomeComponent => () => {
const [sound, setSound] = useState();
const playSound = async () => {
const { sound } = await Audio.Sound.createAsync(
require('../../../assets/sounds/sound.mp3'),
{ shouldPlay: true }
);
setSound(sound);
await sound.playAsync();
};
return <TouchableOpacity onPress={playSound} />
}
版本:
"expo": "^40.0.0",
"expo-av": "~8.7.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
完成后也尝试卸载声音,但仍然无法正常工作:
await sound
.playAsync()
.then(async (playbackStatus) => {
setTimeout(() => {
sound.unloadAsync();
}, playbackStatus.playableDurationMillis);
})
.catch((error) => {
console.log(error);
});