这是我的一些代码:
<TouchableOpacity
style={styles.button}
onPress={this.onPress}
>
<Text> Play Sound </Text>
</TouchableOpacity>
我想编写一个函数“ onPress”,它将播放.mp3声音。 我已经导入了 react-native-sound 并准备好我的.mp3文件,一旦调用onPress函数,我只是不知道如何播放声音。
答案 0 :(得分:1)
我认为,如果您想听声音,可以尝试一下。 语法= react。
Import Sound from "react-native-sound";
Sound.setCategory('Playback');
const whoosh = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
};
whoosh.play((success) => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
reset the player to its uninitialized state (android only)
whoosh.reset();
}
答案 1 :(得分:0)
最简单的方法是首先创建一个新的实例,例如Following。 在构造函数中创建此组件以在组件安装时加载它 注意:请将mp3或wav文件放入android / app / src / main / res / raw
const whoosh = new Sound('swoosh.mp3', Sound.MAIN_BUNDLE);
只需在函数中调用
whoosh.play()
答案 2 :(得分:0)
从react-native-sound导入声音
从'react-native-sound'导入声音;
将mp3文件放入文件夹中,并提及路径:
const requireAudio = require('./ xyz.mp3');
将此代码放在“ onPress”函数中。
const s = new Sound(requireAudio,(e)=> { 如果(e){ console.log('声音错误',e); 返回; } s.play(()=> s.release()); });