我在反应原生音频播放器应用中使用redux和react-redux。它涉及循环一系列音频文件以逐一播放它们。我正在使用递归函数来调度玩家动作。但递归不会被调用。这里有一些代码来解释我的意思:
export function playFile(sectionInfo, sectionArray) {
return (dispatch) => {
const filePath = RNFetchBlob.fs.dirs.DocumentDir + '/courseSections/' + sectionInfo.section.section_id + '.mp3'
Playing = new Sound(filePath,'', error => {
if(error) {
return
}
dispatch(getPlayDuration(Playing.getDuration()))
playPlayList(sectionInfo, sectionArray, dispatch)
})
}
}
function playPlayList(sectionInfo, sectionArray, dispatch) {
dispatch(setCurrentPlaySession(sectionInfo.section))
dispatch(setPlayList(sectionArray))
Playing.play((success) => {
console.log('finished playing ')
Playing.release()
dispatch(completedSection(sectionInfo.section))
sectionArray = sectionArray.slice(1)
if(sectionArray.length > 0) { //move to next file in playlist if playlist hasn't ended
playFile(sectionArray[0],sectionArray) //play next file in playlist
}
})
}
以上是我的行动档案。然后在UI组件中,我将playFile
函数连接到props:
function mapDispatchToProps(dispatch) {
return bindActionCreators({ playFile }, dispatch)
}
我能够从组件中触发一次playFile
功能来播放第一个文件。然后就停止了。我想这是因为从playFile
调用playPlayList
时,它会丢失调度变量?但后来我尝试在playFile(sectionArray[0],sectionArray, dispatch)
中调用playPlayList
,但它仍无效。
非常感谢对此的任何见解,或者我是否应该以完全不同的方式写出来。