2013-04-26 79 views
2

我正在用webgl创建音频可视化工具,并且已经整合了soundcloud音轨。我想不能切换曲目,但是我可以让我的可视化工具工作,并打破音频,或者我可以让音频工作和可视化工具打破。打开音频开关的铬音频分析仪

两种方式,我已经能够使它工作的

音频工作

  1. 删除音频元素
  2. 新的音频元素追加到身体
  3. 触发玩

Vi sualizer工作

  1. 停止音频
  2. 变化源
  3. 触发播放

当我有可视化的工作,音频完全搞砸了。缓冲区听起来不对,音频中有人造物(噪声,哔哔声和爆炸声)。

当我有音频工作,当我打电话analyser.getByteFrequencyData,我得到一个0的数组。我想这是因为分析仪没有正确连接。

的音频使用的代码看起来像

$('#music').trigger("pause"); 
currentTrackNum = currentTrackNum + 1; 
var tracks = $("#tracks").data("tracks") 
var currentTrack = tracks[parseInt(currentTrackNum)%tracks.length]; 
// Begin audio switching 
analyser.disconnect(); 
$('#music').remove(); 
$('body').append('<audio id="music" preload="auto" src="'+ currentTrack["download"].toString() + '?client_id=4c6187aeda01c8ad86e556555621074f"></audio>'); 
startWebAudio(), 

(我不认为我需要pause电话。难道我?)

当我想可视化的工作,我用这代码

currentTrackNum = currentTrackNum + 1; 
var tracks = $("#tracks").data("tracks") 
var currentTrack = tracks[parseInt(currentTrackNum)%tracks.length]; 
// Begin audio switching 
$("#music").attr("src", currentTrack["download"].toString() + "?client_id=4c6187aeda01c8ad86e556555621074f"); 
$("#songTitle").text(currentTrack["title"]); 
$('#music').trigger("play"); 

startWebAudio函数看起来像这样。

function startWebAudio() { 
    // Get our <audio> element 
    var audio = document.getElementById('music'); 
    // Create a new audio context (that allows us to do all the Web Audio stuff) 
    var audioContext = new webkitAudioContext(); 
    // Create a new analyser 
    analyser = audioContext.createAnalyser(); 
    // Create a new audio source from the <audio> element 
    var source = audioContext.createMediaElementSource(audio); 
    // Connect up the output from the audio source to the input of the analyser 
    source.connect(analyser); 
    // Connect up the audio output of the analyser to the audioContext destination i.e. the speakers (The analyser takes the output of the <audio> element and swallows it. If we want to hear the sound of the <audio> element then we need to re-route the analyser's output to the speakers) 
    analyser.connect(audioContext.destination); 

    // Get the <audio> element started 
    audio.play(); 
    var freqByteData = new Uint8Array(analyser.frequencyBinCount); 
} 

我怀疑是分析仪没有勾搭上了正确的,但我想不出来看看弄明白什么。我看过frequencyByteData的输出结果,这似乎表明某些东西没有正确连接。 analyser变量是全局变量。如果您想了解更多的参考代码,这里的where it is on github

回答

1

只能创建每个窗口的单一AudioContext。完成使用后,您还应该断开MediaElementSource

下面是我用来回答类似问题的示例:http://jsbin.com/acolet/1/