react-native-sound 在我的苹果设备上运行良好,但在任何模拟器(iOS 和 android)上都运行良好。奇怪的是,“加载成功”和“声音艺术结束”显示在控制台中,没有任何其他内容。好像 buttonPress.play 不存在。我什么也没听到。在我的设备上,我听到声音并且控制台上写着“成功完成播放”。我记得,它曾经工作过。
import React, { Component } from 'react';
import { TouchableOpacity, Text, View } from 'react-native';
import Sound from 'react-native-sound';
Sound.setCategory('Playback', true);
const playButtonPress = () => {
const buttonPress = new Sound('sound_wrong.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
} else {
console.log('loaded succesfully');
buttonPress.play(success => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
buttonPress.reset();
}
buttonPress.release();
});
console.log('end of soundpart');
}
});
buttonPress.setVolume(1);
};
export default class MyButton extends Component {
render() {
return (
<View style={{ backgroundColor: 'yellow' }}>
<TouchableOpacity onPress={playButtonPress} style={{ marginVertical: 50 }}>
<Text>Click me</Text>
</TouchableOpacity>
</View>
);
}
}
这是我的 package.json
{
"name": "TestAudio",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "17.0.2",
"react-native": "0.66.4",
"react-native-sound": "^0.11.2"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/runtime": "^7.12.5",
"@react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^26.6.3",
"eslint": "7.14.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.66.2",
"react-test-renderer": "17.0.2"
},
"jest": {
"preset": "react-native"
}
}