我正在使用React Native和PHP / MySQL创建播客应用。
我已经将每个播客及其详细信息存储在MySQL数据库中,并且正在使用PHP通过查询获取信息。
然后,我以react native的方式获取信息,并将其转换为JSON格式。
我正在使用本机声音以便在应用程序中播放播客。
这是我的代码。
constructor(props){
super(props);
this.FileUrlHolder = this.FileUrlHolder.bind(this);
this.state = {
playState:'paused', //playing, paused
playSeconds:0,
duration:0,
FileUrlHolder: ''
}
this.sliderEditing = false;
}
componentDidMount(){
fetch('http://da48c35f.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
});
//console.log(responseJson[0].FileUrl);
}).catch((error) => {
console.error(error);
})
play = async () => {
if(this.sound){
this.sound.play(this.playComplete);
this.setState({playState:'playing'});
}else{
const filepath = this.state.FileUrlHolder;
console.log('[Play]', filepath);
this.sound = new Sound(filepath, Sound.MAIN_BUNDLE, (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);
}
});
}
}
我正在尝试在播放功能中访问Fileurl的状态,以便可以使用它来访问要播放的正确文件。
当我运行我的应用程序时,出现以下错误。
TypeError:未定义不是对象(正在评估'this.FileUrlHolder.bind']
我该如何解决?
FileUrlLoader不是您组件的属性,它是一个状态键,因此只需删除该绑定线
仅当您具有名为FileUrlHolder的函数时,才需要this.FileUrlHolder = this.FileUrlHolder.bind(this)