【发布时间】:2016-08-28 22:14:41
【问题描述】:
我正在学习 webgl,我正在通过 this 教程从 mp3 文件中提取频率,以便将它们可视化。
我让它在给定一个 mp3 文件的地方工作。但是如果我尝试用createMediaElementSource连接AudioContext的分析仪来获取频率数据,就不行了。
JS:
window.onload = function(e) {
document.getElementById('music-files').addEventListener('change', selectMusic, false);
}
var musicFiles = [];
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var audio;
var audioSrc;
var analyser;
var bufferLength;
var dataArray;
function selectMusic(e) {
musicFiles = e.target.files;
}
function getFreq(){
requestAnimationFrame(getFreq);
analyser.getByteFrequencyData(dataArray);
console.log(">>>>>>>>>>>>>>>");
console.log(dataArray[240])
}
function play(){
var num = Math.floor(Math.random()*musicFiles.length);
console.log("playing=" + num);
var musicFile = URL.createObjectURL(musicFiles[num]);
$("#music").attr("src", musicFile);
document.getElementById('music').play();
audio = document.getElementById('music');
audioSrc = audioCtx.createMediaElementSource(audio);
analyser = audioCtx.createAnalyser();
audioSrc.connect(analyser);
bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
getFreq();
}
function stop(){
document.getElementById('music').pause();
}
如果你看一下小提琴,你可以选择一个 mp3 文件并点击播放,它会在浏览器控制台中记录一个频段但没有声音(至少在我的电脑上),这可能意味着它正在播放歌曲但没有声音。但是,如果您在下面评论这些行,它会播放歌曲并且我可以听到。
/*audioSrc = audioCtx.createMediaElementSource(audio);
analyser = audioCtx.createAnalyser();
audioSrc.connect(analyser);
bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
getFreq();*/
我的设置:Windows 10/Chrome52
对此问题的任何建议表示赞赏。谢谢。
【问题讨论】:
-
它是否说明了浏览器控制台中的 CORS 限制?
-
不。它没有说任何关于 CORS 的内容。