将音频缓冲区转换为音频元素

时间:2018-04-07 18:25:01

标签: javascript soundcloud web-audio-api

我正在使用souncloud api从soundcloud流式传输音频。但是,我正在尝试以编程方式获取跟踪BPM,所以我有以下

Soundcloud.prototype.play = function (options) {
  options = options || {};
  var src;

  if (options.streamUrl) {
    src = options.streamUrl;
  } else if (this._playlist) {
    var length = this._playlist.tracks.length;
    if (length) {
      if (options.playlistIndex === undefined) {
        this._playlistIndex = this._playlistIndex || 0;
      } else {
        this._playlistIndex = options.playlistIndex;
      }

      // be silent if index is out of range
      if (this._playlistIndex >= length || this._playlistIndex < 0) {
        this._playlistIndex = 0;
        return;
      }
      src = this._playlist.tracks[this._playlistIndex].stream_url;
    }
  } else if (this._track) {
    src = this._track.stream_url;
  }

  if (!src) {
    throw new Error('There is no tracks to play, use `streamUrl` option or `load` method');
  }

  if (this._clientId) {
    src = _appendQueryParam(src, 'client_id', this._clientId);
  }

  if (src !== this.audio.src) {
    pulse.loadBufferFromURI(src, (event, pulse) => {
      this.trackBPM = pulse.beat.bpm
      this.audio.src = window.URL.createObjectURL(pulse.renderedBuffer);
      this.playing = src;
      console.log('trackBPM',pulse.beat.bpm);
    })
  } else return this.audio.play();
};

现在

pulse.loadBufferFromURI(src, (event, pulse) => {
          this.trackBPM = pulse.beat.bpm
          this.audio.src = window.URL.createObjectURL(pulse.renderedBuffer);
          this.playing = src;
          console.log('trackBPM',pulse.beat.bpm);
        })

尝试将stream_url加载为aduiobuffer以获取bpm。问题是我可以将缓冲区转换为足够兼容的源,以便使用<audio>元素播放,原因是我想利用事件监听器......

0 个答案:

没有答案