我正在努力在我用 Expo 创建的应用程序中实现一些非常简单的音频播放功能。目前有两段音频,它们在 IOS(真实设备和模拟器)上都可以正常播放和停止。在 Android 上进行测试时,音频会播放一段时间,然后在达到完整持续时间之前停止(通常每个轨道总共大约 30 秒。两个轨道之间的完整持续时间从 4 到 8 分钟不等。)
这是我的一些代码片段,如果需要更多,请告诉我:
const sounds = {
one: require('../asset/sound1.mp3'),
two: require('../asset/sound2.mp3'),
}
常量 [soundObj, setSoundObj] = useState(null);
handlePlaySound = async track => {
if (soundObj === null) {
setSoundObj(new Audio.Sound());
}
try {
let source = sounds[track]
await soundObj.loadAsync(source)
await soundObj
.playAsync()
.then(async playbackStatus => {
setTimeout(() => {
soundObj.unloadAsync()
}, playbackStatus.playableDurationMillis)
}).catch(error => {
console.log(error)
})
} catch (error) { console.log(error) }
}
handleStopSound = async () => {
await soundObj.stopAsync(); setSoundObj(null);
}
SDK 版本:4.0.11 平台:Android/iOS