【发布时间】:2021-05-06 15:58:55
【问题描述】:
我尝试只使用一个按钮来播放和暂停音乐。当按下按钮时音乐开始播放,但当尝试暂停音乐时出现问题。我做错了什么?
我收到以下错误:
可能的未处理的 Promise Rejection (id:0): TypeError: Sound.pauseAsync 不是函数。
这是我的音频功能
const { sound } = await Audio.Sound.createAsync(
{ uri: props.songURL }
);
await Audio.setIsEnabledAsync(true);
setSound(songs);
setSongs(userSongPosts)
if (isPlaying === true) {
try {
await sound.pauseAsync();
setIsPlaying(false);
} catch (error) {
console.error(error);
}
} else {
try {
await sound.playAsync();
setIsPlaying(true);
} catch (error) {
console.error(error);
}
}
}
这是我的按钮
<TouchableOpacity
style={styles.profilContainer}
onPress={() => playSound() }>
<Text>{isPlaying ? 'Pause' : 'Play'}</Text>
<View style={styles.buttonView}>
<Text style={styles.textButton}>{props.submitText}</Text>
<MatetrialCommunityIcons
style={styles.iconStyles}
name={props.icon}
/>
【问题讨论】:
-
那么到底是什么不起作用?你有错误吗?
-
我无法暂停音乐。我在问题中添加了错误消息,请参阅更新。
标签: react-native expo media-player