【发布时间】:2018-08-21 19:02:49
【问题描述】:
我想创建一个关闭按钮,停止当前播放的歌曲并在指定的时间(比如说 5 分钟)后播放它我应该怎么做?这是我的代码:
onPressButtonPlay() {
if(song != null) {
song.play((success) => {
if(!success)
ToastAndroid.show('Error when play SoundPlayer :(((', ToastAndroid.SHORT);
});
}
}
onPressButtonDismiss() {
if(song != null) {
song.stop((success) => {
if(!success)
ToastAndroid.show('Alarm dismissed', ToastAndroid.SHORT);
});
}
}
onPressButtonStop() {
if(song != null) {
song.stop((success) => {
if(!success)
ToastAndroid.show('Alarm stopped', ToastAndroid.SHORT);
});
}
}
componentDidMount() {
this.timeoutCheck = setTimeout(() => {
this.setTimePassed();
}, 400);
}
setTimePassed() {
this.setState({timePassed: true});
}
onPressButtonTimer() {
if (!this.state.timePassed){
song.play();
}
}
【问题讨论】:
-
你在使用 redux 吗?
-
是的。所有模块都工作正常,只是不知道如何编写正确的代码。
-
我可以建议添加 redux-saga 并在那里处理计时器吗?他们有一个很好的延迟功能,您可以等待延迟,然后在时间过去后继续前进并调用下一个功能。
-
难道不能用后台定时器来做到这一点吗?如果没有,我的代码在使用 redux-saga 时应该是什么样子?
-
我从未使用过后台计时器,这看起来是一个有趣的解决方案。我认为这可能比我在我的应用程序上使用的 redux 架构更适合您构建它的方式。
标签: reactjs react-native timer background task