Javascript,流式播放非';不仅仅是mp3?e、 g youtube

Javascript,流式播放非';不仅仅是mp3?e、 g youtube,javascript,audio,web,Javascript,Audio,Web,所以,我想流式播放不仅仅是mp3文件的东西。 比如说youtube 忽略代码中除音频部分以外的其他部分。这是一个可视化工具,我从一个制作了非常好的音频可视化工具的家伙那里改造过来 我将如何让它播放youtube文件和流式链接的直接链接。我想做的是添加一个随机歌曲列表,这样它就可以从每首歌曲中流取一首歌曲,而不是我将100首歌曲上传到我的域中,浪费空间 (function() { var ALPHA, AudioAnalyser, COLORS, MP3_PATH, NUM_BANDS, NU

所以,我想流式播放不仅仅是mp3文件的东西。 比如说youtube

忽略代码中除音频部分以外的其他部分。这是一个可视化工具,我从一个制作了非常好的音频可视化工具的家伙那里改造过来

我将如何让它播放youtube文件和流式链接的直接链接。我想做的是添加一个随机歌曲列表,这样它就可以从每首歌曲中流取一首歌曲,而不是我将100首歌曲上传到我的域中,浪费空间

(function() {
  var ALPHA, AudioAnalyser, COLORS, MP3_PATH, NUM_BANDS, NUM_PARTICLES, Particle, SCALE, SIZE, SMOOTHING, SPEED, SPIN;

  NUM_PARTICLES = 150;

  NUM_BANDS = 128;

  SMOOTHING = 0.5;

  var MP3_PATH = ['website.com/Song.mp3', 'website.com/Song2.mp3', 'website.com/Song3.mp3'];


  SCALE = {
    MIN: 5.0,
    MAX: 20.0
  };

  SPEED = {
    MIN: 0.2,
    MAX: 1.0
  };

  ALPHA = {
    MIN: 0.8,
    MAX: 0.9
  };

  SPIN = {
    MIN: 0.001,
    MAX: 0.005
  };

  SIZE = {
    MIN: 0.5,
    MAX: 0.90
  };

  COLORS = ['#69D2E7', '#1B676B', '#BEF202', '#EBE54D', '#00CDAC', '#1693A5', '#F9D423', '#FF4E50', '#E7204E', '#0CCABA', '#FF006F'];

  AudioAnalyser = (function() {
    AudioAnalyser.AudioContext = self.AudioContext || self.webkitAudioContext;

    AudioAnalyser.enabled = AudioAnalyser.AudioContext != null;

    function AudioAnalyser(audio, numBands, smoothing) {
      var src;
      this.audio = audio != null ? audio : new Audio();
      this.numBands = numBands != null ? numBands : 256;
      this.smoothing = smoothing != null ? smoothing : 0.3;
      if (typeof this.audio === 'string') {
        src = this.audio;
        this.audio = new Audio();
        this.audio.crossOrigin = "anonymous";
        this.audio.controls = true;
        this.audio.src = src;
      }
      this.context = new AudioAnalyser.AudioContext();
      this.jsNode = this.context.createScriptProcessor(2048, 1, 1);
      this.analyser = this.context.createAnalyser();
      this.analyser.smoothingTimeConstant = this.smoothing;
      this.analyser.fftSize = this.numBands * 2;
      this.bands = new Uint8Array(this.analyser.frequencyBinCount);
      this.audio.addEventListener('canplay', (function(_this) {
        return function() {
          _this.source = _this.context.createMediaElementSource(_this.audio);
          _this.source.connect(_this.analyser);
          _this.analyser.connect(_this.jsNode);
          _this.jsNode.connect(_this.context.destination);
          _this.source.connect(_this.context.destination);
          return _this.jsNode.onaudioprocess = function() {
            _this.analyser.getByteFrequencyData(_this.bands);
            if (!_this.audio.paused) {
              return typeof _this.onUpdate === "function" ? _this.onUpdate(_this.bands) : void 0;
            }
          };
        };
      })(this));
    }

    AudioAnalyser.prototype.start = function() {
      return this.audio.play();
    };

    AudioAnalyser.prototype.stop = function() {
      return this.audio.pause();
    };

    return AudioAnalyser;

  })();

  Particle = (function() {
    function Particle(x1, y1) {
      this.x = x1 != null ? x1 : 0;
      this.y = y1 != null ? y1 : 0;
      this.reset();
    }

    Particle.prototype.reset = function() {
      this.level = 1 + floor(random(4));
      this.scale = random(SCALE.MIN, SCALE.MAX);
      this.alpha = random(ALPHA.MIN, ALPHA.MAX);
      this.speed = random(SPEED.MIN, SPEED.MAX);
      this.color = random(COLORS);
      this.size = random(SIZE.MIN, SIZE.MAX);
      this.spin = random(SPIN.MAX, SPIN.MAX);
      this.band = floor(random(NUM_BANDS));
      if (random() < 0.5) {
        this.spin = -this.spin;
      }
      this.smoothedScale = 0.0;
      this.smoothedAlpha = 0.0;
      this.decayScale = 0.0;
      this.decayAlpha = 0.0;
      this.rotation = random(TWO_PI);
      return this.energy = 0.0;
    };

    Particle.prototype.move = function() {
      this.rotation += this.spin;
      return this.y -= this.speed * this.level;
    };

    Particle.prototype.draw = function(ctx) {
      var alpha, power, scale;
      power = exp(this.energy);
      scale = this.scale * power;
      alpha = this.alpha * this.energy * 1.5;
      this.decayScale = max(this.decayScale, scale);
      this.decayAlpha = max(this.decayAlpha, alpha);
      this.smoothedScale += (this.decayScale - this.smoothedScale) * 0.3;
      this.smoothedAlpha += (this.decayAlpha - this.smoothedAlpha) * 0.3;
      this.decayScale *= 0.985;
      this.decayAlpha *= 0.975;
      ctx.save();
      ctx.beginPath();
      ctx.translate(this.x + cos(this.rotation * this.speed) * 250, this.y);
      ctx.rotate(this.rotation);
      ctx.scale(this.smoothedScale * this.level, this.smoothedScale * this.level);
      ctx.moveTo(this.size * 0.5, 0);
      ctx.lineTo(this.size * -0.5, 0);
      ctx.lineWidth = 1;
      ctx.lineCap = 'round';
      ctx.globalAlpha = this.smoothedAlpha / this.level;
      ctx.strokeStyle = this.color;
      ctx.stroke();
      return ctx.restore();
    };

    return Particle;

  })();

  Sketch.create({
    particles: [],
    setup: function() {
      var analyser, error, i, intro, j, particle, ref, warning, x, y;
      for (i = j = 0, ref = NUM_PARTICLES - 1; j <= ref; i = j += 1) {
        x = random(this.width);
        y = random(this.height * 2);
        particle = new Particle(x, y);
        particle.energy = random(particle.band / 350);
        this.particles.push(particle);
      }
      if (AudioAnalyser.enabled) {
        try {
          analyser = new AudioAnalyser(random(MP3_PATH), NUM_BANDS, SMOOTHING);
          analyser.onUpdate = (function(_this) {
            return function(bands) {
              var k, len, ref1, results;
              ref1 = _this.particles;
              results = [];
              for (k = 0, len = ref1.length; k < len; k++) {
                particle = ref1[k];
                results.push(particle.energy = bands[particle.band] / 350);
              }
              return results;
            };
          })(this);
          analyser.start();
          document.body.appendChild(analyser.audio);
          intro = document.getElementById('intro');
          intro.style.display = 'none';
          if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) {
            warning = document.getElementById('warning2');
            return warning.style.display = 'block';
          }
        } catch (_error) {
          error = _error;
        }
      } else {
        warning = document.getElementById('warning1');
        return warning.style.display = 'block';
      }
    },
    draw: function() {
      var j, len, particle, ref, results;
      this.globalCompositeOperation = 'lighter';
      ref = this.particles;
      results = [];
      for (j = 0, len = ref.length; j < len; j++) {
        particle = ref[j];
        if (particle.y < -particle.size * particle.level * particle.scale * 2) {
          particle.reset();
          particle.x = random(this.width);
          particle.y = this.height + particle.size * particle.scale * particle.level;
        }
        particle.move();
        results.push(particle.draw(this));
      }
      return results;
    }
  });

}).call(this);
(函数(){
var ALPHA、音频分析仪、颜色、MP3_路径、NUM_波段、NUM_粒子、粒子、比例、大小、平滑、速度、旋转;
粒子数=150;
频带数=128;
平滑度=0.5;
var MP3_PATH=['website.com/Song.MP3','website.com/Song2.MP3','website.com/Song3.MP3'];
比例={
最低:5.0,
最高:20.0
};
速度={
最小值:0.2,
最高:1.0
};
α={
最小值:0.8,
最高:0.9
};
自旋={
最小值:0.001,
最高:0.005
};
尺寸={
最小值:0.5,
最高:0.90
};
颜色=['69D2E7'、'1B676B'、'BEF202'、'EBE54D'、'00CDAC'、'1693A5'、'F9D423'、'FF4E50'、'E7204E'、'0CABA'、'FF006F'];
音频分析器=(函数(){
AudioAnalyzer.AudioContext=self.AudioContext | | self.webkitAudioContext;
audioanalyzer.enabled=audioanalyzer.AudioContext!=null;
功能音频分析仪(音频、数字、平滑){
var-src;
this.audio=audio!=null?audio:new audio();
this.numBands=numBands!=null?numBands:256;
this.smoothing=平滑!=null?平滑:0.3;
if(this.audio的类型==='string'){
src=this.audio;
this.audio=新音频();
this.audio.crossOrigin=“匿名”;
this.audio.controls=true;
this.audio.src=src;
}
this.context=新的AudioAnalyzer.AudioContext();
this.jsNode=this.context.createScriptProcessor(2048,1,1);
this.analyzer=this.context.createAnalyzer();
this.analyzer.smoothingTimeConstant=this.smoothing;
this.analyzer.fftSize=this.numBands*2;
this.bands=新UINT8阵列(this.analyzer.frequencyBinCount);
this.audio.addEventListener('canplay'),(函数(\u this){
返回函数(){
_this.source=_this.context.createMediaElementSource(_this.audio);
_本源连接(本分析仪);
_this.analyzer.connect(_this.jsNode);
_this.jsNode.connect(_this.context.destination);
_this.source.connect(_this.context.destination);
返回_this.jsNode.onaudioprocess=function(){
_本分析仪获得的频率数据(_本频带);
如果(!\u此音频已暂停){
返回类型为_this.onUpdate==“function”?_this.onUpdate(_this.bands):void 0;
}
};
};
})(本);;
}
audioanalyzer.prototype.start=函数(){
返回此.audio.play();
};
audioanalyzer.prototype.stop=函数(){
返回此.audio.pause();
};
返回式音频分析仪;
})();
粒子=(函数(){
功能粒子(x1,y1){
这个.x=x1!=null?x1:0;
这个.y=y1!=null?y1:0;
这是reset();
}
Particle.prototype.reset=函数(){
该层=1+楼层(随机(4));
this.scale=随机(scale.MIN,scale.MAX);
this.alpha=随机(alpha.MIN,alpha.MAX);
this.speed=随机(speed.MIN,speed.MAX);
this.color=随机(颜色);
this.size=随机(size.MIN,size.MAX);
this.spin=随机(spin.MAX,spin.MAX);
this.band=地板(随机(NUM_BANDS));
如果(随机()<0.5){
this.spin=-this.spin;
}
此.SmoothdScale=0.0;
该值为0.0;
这1.decadyscale=0.0;
该值为0.0,十雅法=0.0;
此旋转=随机(两个π);
返回该值。能量=0.0;
};
Particle.prototype.move=函数(){
this.rotation+=this.spin;
返回this.y-=this.speed*this.level;
};
Particle.prototype.draw=函数(ctx){
varα、功率、标度;
功率=exp(该能量);
比例=此。比例*功率;
α=这个α*这个能量*1.5;
this.decascale=最大值(this.decascale,scale);
this.decayAlpha=最大值(this.decayAlpha,alpha);
this.smoothdscale+=(this.decayScale-this.smoothdscale)*0.3;
this.smoothDalpha+=(this.decayAlpha-this.smoothDalpha)*0.3;
这个10.decadyscale*=0.985;
此值为0.decayAlpha*=0.975;
ctx.save();
ctx.beginPath();
ctx.translate(this.x+cos(this.rotation*this.speed)*250,this.y);
ctx.旋转(此旋转);
ctx.scale(this.smoothdscale*this.level,this.smoothdscale*this.level);
ctx.moveTo(该尺寸*0.5,0);
ctx.lineTo(该尺寸*-0.5,0);
ctx.lineWidth=1;
ctx.lineCap='圆形';
ctx.globalAlpha=this.smoothdalpha/this.level;
ctx.strokeStyle=this.color;
ctx.stroke();
返回ctx.restore();
};
返回粒子;
})();
草图。创建({
粒子:[],
设置:函数(){
var分析仪,误差,i,简介,j,粒子,参考,警告,x,y;
对于(i=j=0,ref=NUM_粒子-1;j