首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取Visualizer的音轨

如何获取Visualizer的音轨
EN

Stack Overflow用户
提问于 2019-01-21 23:49:12
回答 1查看 432关注 0票数 1

我正在尝试创建一个音频可视化工具,它将根据正在播放的音频进行脉动。我已经使用three.js创建了一个3D模型,目前它是随机脉冲的,但我在理解如何获取音频文件本身时遇到了麻烦。

我使用this repo作为提示,但是我被App.js文件卡住了。这段代码的目的是通过麦克风从用户那里获取输入,而我在理解用于获取音频文件的WebAPI文档时遇到了问题。

我如何让应用程序从文件而不是麦克风中获取音频?

编辑:

audio.js

代码语言:javascript
运行
复制
import React, { Component } from 'react';
import AudioAnalyser from './audioAnalyser';

import song from './Teehee.mp3';

class Audio extends Component {
  constructor(props) {
    super(props);
    this.state = {
        audioStatus: 'PAUSED'
    };
    this.audioEle = null;
    this.songName = 'Blues in A';
  }

  componentDidMount() {
    this.audioEle = document.getElementById('audio-element');
    this.audioEle.oncanplay = (e) => {
      // safari
      if (this.isSafari()) {
        return
      }
      this.play()
    }
  }

  isSafari = () => {
    return window.navigator.userAgent.indexOf('Safari') > -1 && window.navigator.userAgent.indexOf('Chrome') === -1
  }

  pause = () => {
    this.audioEle.pause()
    this.setState({
      audioStatus: 'PAUSED'
    })
  }
  play = () => {
    this.audioEle.play()
    this.setState({
      audioStatus: 'PLAYING'
    })
    console.log(this.state);

  }

  toggleMusic = () =>  {

    console.log(this.state);

    if (this.state.audioStatus === 'PLAYING') {
      this.pause();
    } else {
      this.play();
    }
  }

  render() {
    return (
      <div className="AudioPlayer">        
        <audio id="audio-element" preload="true" src={`${song}`} crossorigin="anonymous" ></audio>
        <div className="controls">
          <button onClick={this.toggleMusic}>
            {this.state.audioStatus ? 'PAUSED' : 'PLAYING'}
          </button>
        </div>
        {this.state.audioStatus ? <AudioAnalyser audio={this.state.audioStatus} /> : ''}
      </div>
    );
  }
}

export default Audio;

audioAnalyser.js

代码语言:javascript
运行
复制
/*
This component will analyse an audio file using the Web Audio API, more information can be found here:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API

https://www.twilio.com/blog/audio-visualisation-web-audio-api--react

*/

import React, { Component } from 'react';
import AudioVisualiser from './audioVisualizer';

class AudioAnalyser extends Component {

    constructor(props) {
        super(props);
        this.state = { audioData: new Uint8Array(0) };
        this.tick = this.tick.bind(this);
      }


    // When Component mounts, set up Web Autio API objets.
    componentDidMount() {

        // The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.
        this.audioContext = new (window.AudioContext ||  window.webkitAudioContext)();

        // The AnalyserNode interface represents a node able to provide real-time frequency and time-domain analysis information.
        this.analyser = this.audioContext.createAnalyser();

        // This dataArray will be used to store the waveform data that the AnalyserNode will be creating.
        this.dataArray = new Uint8Array(this.analyser.frequencyBinCount);


        console.log(this.props);

        console.log(this.props.audio);
        // The source, which is the audio file.
        this.source = this.audioContext.createMediaStreamSource(this.props.audio);

        // connect source to the anlayser .
        this.source.connect(this.analyser);
      }

      // Method that will be called every time requestAnimationFrame runs. 
      // The function will copy the current waveform as an array of integers, from the AnalyserNode into the dataArray. 
      // It will then update the audioData property in the component's state with the dataArray. 
      // Finally, it will call on requestAnimationFrame again to request the next update.
      tick() {

        // Use the analyser to update the visualization from the dataArray
        this.analyser.getByteTimeDomainData(this.dataArray);

        // 
        this.setState({ audioData: this.dataArray });

        this.rafId = requestAnimationFrame(this.tick);


      }

      componentWillUnmount() {
        cancelAnimationFrame(this.rafId);
        this.analyser.disconnect();
        this.source.disconnect();
      }

      render() {
        return <AudioVisualiser audioData={this.state.audioData} />;
      }
    }

    export default AudioAnalyser;
EN

回答 1

Stack Overflow用户

发布于 2019-01-21 23:59:54

您可以使用input element with type file,然后使用File API访问音频文件,并将其添加到应用的状态。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54293419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档