React未捕获(承诺中)DOMException:play()请求被新的加载请求中断

问题描述:

我正尝试使用ReactJS为我的UI加载队列系统的视频。但是,当我设置状态来更改视频的URL时,我得到错误“未捕获(承诺)DOMException:play()请求被新的加载请求中断。”React未捕获(承诺中)DOMException:play()请求被新的加载请求中断

这里是我的代码:

class Videoplayer extends Component { 
    constructor(props){ 
     super(props); 
     this.state = { 
      queue: ['fi4s9uwrg9'] 
     } 
    } 
    componentWillMount(){ 
     fetch(/api).then((res)=>{ 
      res.json().then((data)=>{ 
       let hashedqueue = []; 
       data.forEach((vid)=>hashedqueue.push(vid.hashed_id)); 
       this.setState({ 
        queue: hashedqueue 
       })   
      }) 
     }) 
    } 
    finishedPlaying(){ 
     let videos = this.state.queue; 
     videos.shift(); 
     this.setState({queue:videos}) //this is the line thats causing the issue 
    } 

    render(){  
     return(
     <div className="videoplayer"> 
      <ReactPlayer ref={player => { this.player = player }} onEnded={()=>this.finishedPlaying()} url={'/videosource/'+this.state.queue[0]} playing /> 
     </div> 
     ) 
    } 
} 

顺便说hashedid本质上是视频的ID

编辑:我使用这个包:https://www.npmjs.com/package/react-player的视频播放器,所以我可以使用wistia与反应

如果您需要访问状态进行状态更新,改用回调:

finishedPlaying() { 
    this.setState(prevState => { 
     // make a shallow copy so as not to mutate state 
     const videos = [...prevState.queue]; 
     videos.shift(); 
     return { queue: videos }; 
    }); 
    } 
+0

这仍然给我同样的错误,但在队列中向前跳两个视频。也许这是因为你有两个变量,let和const视频。 – moesh

+0

好的,忘记删除'setState'上面的那些行。 –

+0

嗯,现在当第一个结束但第三个(或之后)没有进入第二个视频。似乎onEnd的道具不会再被调用 – moesh