"react-native": "0.66.4", react-native-sound": "^0.11.1",
每当用户单击一个项目时,我想阻止其他按钮音乐,
sound.reset() - 在 else 方法中使用这个,我遇到了这样的错误:TypeError: sound.reset is not a function。(在 'sound.reset()' 中,'sound.reset' 未定义)
///play music function when user click on a specific item
// Play a music
const playSound = ({item, index}) => {
for (var i = 0; i < items.length; i++) {
if (item.id === items[i].id) {
sound = new Sound(item.uri, error => {
if (error) {
console.log('failed to load the sound', error);
return;
}
// loaded successfully
// setTimeout(() => stopFunction(), 3000);
const stopFunction = () => {
sound.release();
sound.reset();
};
sound.play(success => {
if (success) {
console.log('successfully finished playing');
// Release when it's done so we're not using up resources
sound.release();
} else {
console.log('playback failed due to audio decoding errors');
}
});
});
} else {
sound.reset();
// sound.stop();
}
}
};
/// main function
<FlatGrid
itemDimension={90}
data={items}
style={styles.gridView}
// staticDimension={300}
// fixed
spacing={5}
renderItem={({item, index, data}) => {
return (
<TouchableOpacity
style={styles.itemContainer}
activeOpacity={0.3}
onPress={() => {
playSound({item, index});
}}>
<Text style={styles.itemName}>{item.title}</Text>
</TouchableOpacity>
);
}}
/>