【问题标题】:How to create a playlist using lis in ReactJS如何在 ReactJS 中使用 lis 创建播放列表
【发布时间】:2019-12-17 20:24:26
【问题描述】:

我正在尝试创建一个 UI,允许用户将 mp3 从他们的计算机下载到播放列表。现在我已经得到了歌曲的名称和指向正确歌曲的 li 的 href。但是现在我想知道每次用户单击 li 时如何在音频元素上播放歌曲。现在是这样的:

我的状态是这样的:

这是我的代码:

class DownloadTest extends React.Component {
  constructor(props) {
    super(props);

    this.handleClick = this.handleClick.bind(this);

    this.inputRef = React.createRef();

    this.state = {
      files: []
    };
  }



handleClick = event => {

  // Helper code to read file and return promise
  const readFile = (file) => {

    const fileList = [];

    const fileReader = new FileReader();

    // create the promise and return it
    return new Promise((resolve, reject) => {

      // if file reader has an error, report it
      fileReader.onerror = (error) => {
        reject({ error })
      }

      // if success, resolve the promise
      fileReader.onload = (e) => {
        resolve({
          name: file.name,
          link: e.target.result
        })
      }

      // start reading the file
      fileReader.readAsDataURL(file);

    })
  }

  // create all the file reader promises
  // create an array from the files list and use map to generate
  // an array of promises
  const allReaders = Array.from(event.target.files).map(readFile)

  // Now handle the array of promises we just created
  Promise.all(allReaders)
    .then(fileList => {
      console.log(fileList)
      // set the state that we have all the files
      this.setState({ files: fileList });
    })
    .catch(error => { 
       console.error(error)
    });

}

  render() {
     console.log(this.state);
    return (
      <div className="downloadMusic">
      <div className="input">
        <input
          onChange={this.handleClick}
          id="upload-file"
          className="inputName"
          type="file"
          multiple
          ref={this.inputRef}
        />
        </div>
        <div className="audio-player">
        <audio
         src="my_audio_file.ogg"
         autoPlay
         controls
         />
         </div>

        <div>
          <ul ref={this.ulRef}>
            {this.state.files.map((file, index) => (
              <li key={index}>
                <a href={file.link}>{file.name}</a>
              </li>
            ))}
          </ul>
        </div>
      </div>
    );
  }
}

export default DownloadTest;

【问题讨论】:

  • 你不能只改变音​​频元素中的文件,就像用 react 改变图像一样吗?我认为您不需要文件阅读器? &lt;audio src={this.state.file}/&gt;
  • @Kokodoko 你能详细说明一下吗?
  • 我创建了一个测试,这有效????

标签: javascript reactjs filereader


【解决方案1】:

更新的答案,测试和工作。您可以使用组件状态来更改音频元素来源。

import React from "react";
import { render } from "react-dom";
import Joy from "./joy.wav"
import Alive from "./alive1.wav"

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      audio: Joy
    }
  }

  render() {
    return (
      <div>
        <button onClick={() => this.setState({ audio: Alive })}>
          Change audio
        </button>
        <audio src={this.state.audio} controls />
      </div>
    )
  }
}

【讨论】:

  • 接受答案是什么意思?你想让我把它作为正确的答案吗?
  • 谢谢,哈哈? 这就是 stackoverflow 的工作原理,否则它会作为一个未回答的问题保持可见。
猜你喜欢
  • 1970-01-01
  • 2012-08-14
  • 2020-09-30
  • 2017-11-07
  • 1970-01-01
  • 2015-10-01
  • 2022-01-24
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多