【问题标题】:React - play media after items are loaded with array.map()React - 在使用 array.map() 加载项目后播放媒体
【发布时间】:2020-03-06 16:15:29
【问题描述】:

我使用react-player 来渲染带有map() 的youtube 视频,如下所示:

class Jukebox extends Component {
  constructor (props) {
    super(props);
    this.state = {
     youtube_urls:[],      
     clients:[],        
    };
  };

  componentDidMount() {
    if (this.props.isAuthenticated) {
      this.getItems();
    }
  };

  getItems(event) {
    const {userId} = this.props
    const options = {
      url: `${process.env.REACT_APP_WEB_SERVICE_URL}/jukebox/${userId}`,
      method: 'get',
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': true,
        Authorization: `Bearer ${window.localStorage.authToken}`
      }
    };
    return axios(options)
    .then((res) => { 
      console.log(res.data)  
      console.log(res.data.data) 
      this.setState({
        clients: res.data.data[0].clients,
        youtube_urls:res.data.data[0].youtube_urls
      })
    })
    .catch((error) => { console.log(error); });
  };

render() {
 const { youtube_urls } = this.state;
 return (
  <div>      
   <h1 className="title is-1">Jukebox</font></h1>
  {
   clients.map((client, index) => {
     /* 
    Obtain preview from state.previews for current artist index 
    */
    const audio = youtube_urls[index]
    /* 
    Render current client data, and corresponding audio url
    */
    return(
      <div key={index}>
      <ul>
        <ReactPlayer 
          url={ audio }
          controls
          playing // <--- does not work
          width='50'
          height='150'
        />
        <div className="Line" />
      </ul></div>
     )
   })
 })
};

根据库道具之一playing(见上文),您可以在渲染完成时自动播放媒体,但如果我使用map(),这将导致所有视频同时播放。

同时还有回调proponReady()

当媒体加载并准备好播放时调用。如果播放设置为 true,媒体将立即播放。


问题:

如何实现这一点并在加载所有媒体后播放所有视频,从索引 0 开始,一次播放一个?

【问题讨论】:

    标签: reactjs react-player


    【解决方案1】:

    我会使用两个状态变量:videosLoadedCountplayingIndex。在0playingIndex-1 初始化videosLoadedCount

    你可以从playingIndex状态值中推断出正在播放的道具。

    只要有 onReady,就增加 videosLoadedCount。当达到视频数量时,您可以增加playingIndex。每当有onEnded 回调时,您就递增playingIndex


    这样的事情应该可以工作:

    class Jukebox extends Component {
      constructor(props) {
        super(props);
        this.state = {
          loadedVideosCount: 0,
          currentPlayingIndex: -1,
        };
      }
    
      render() {
        const { youtube_urls } = this.props;
        const { loadedVideosCount, currentPlayingIndex } = this.state;
    
        return (
          <div>
            {youtube_urls.map((url, index) => (
              <ReactPlayer
                url={url}
                controls
                onLoaded={() =>
                  this.setState(currentState => ({
                    loadedVideosCount: loadedVideosCount + 1,
                    currentPlayingIndex:
                      loadedVideosCount + 1 === youtube_urls.length ? 0 : -1,
                  }))
                }
                onEnded={() =>
                  this.setState(currentState => ({
                    currentPlayingIndex: currentPlayingIndex + 1,
                  }))
                }
                playing={index === currentPlayingIndex} // <--- does not work
                width="50"
                height="150"
              />
            ))}
          </div>
        );
      }
    }
    

    【讨论】:

    • 你能用代码吗?我已经添加了我的构造函数。我会接受并为善意投票
    • 为什么要把 youtube_urls 从 state 改成 props?
    • 由于语法不正确,我从头开始重新创建组件。这没有多大意义,因为他们无法将视频添加到 youtube_urls 并将其作为状态可能会使问题复杂化
    • 为了完整起见,我已经编辑了我的组件,以便您了解我的状态。还是没有道理?我从clients 映射,而不是yoube_urls,用户客户端索引...以防它帮助你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多