我正在尝试创建一个反应原生的应用程序。我将信息存储在数据库中。我使用 php 通过查询来获取数据库的信息。
然后我使用 fetch 来获取信息,我能够获取信息并将其显示为文本。我想将数据作为变量,以便我可以将它与反应原生声音一起使用。
这是我的反应原生代码
constructor(props){
super(props);
this.state = {
playState:'paused', //playing, paused
playSeconds:0,
duration:0,
FileUrlHolder: ''
}
this.sliderEditing = false;
}
componentDidMount(){
fetch('http://f10f7e2e.ngrok.io/Filter.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
// Getting the id.
id: this.props.navigation.state.params.FlatListClickItemHolder
})
}).then((response) => response.json())
.then((responseJson) => {
this.setState({
FileUrlHolder: responseJson[0].FileUrl
});
}).catch((error) => {
console.error(error);
});
}
componentWillUnmount(){
play = async () => {
if(this.sound){
this.sound.play(this.playComplete);
this.setState({playState:'playing'});
}else{
const filepath = this.state.FileUrlHolder;
console.log('[Play]', filepath);
//Alert.alert('id ' + filepath);
this.sound = new Sound(filepath, (error) => {
if (error) {
console.log('failed to load the sound', error);
Alert.alert('Notice', 'audio file error. (Error code : 1)');
this.setState({playState:'paused'});
}else{
this.setState({playState:'playing', duration:this.sound.getDuration()});
this.sound.play(this.playComplete);
}
});
}
}
我的 json 看起来像
[{"FileUrl":"John_30th_sept_2018.mp3"}]