有一个自定义的动画HTML音频分析器图形,该图形播放音轨并根据该音频的频率移动。
这是CodePen HERE中的代码,我发现它仅适用于HTTPS域。
现在的问题是:我不需要像代码一样上载音轨,只需要一个简单的音轨即可自动播放并删除该上载按钮。假设我在服务器的同一目录中有一个 track1.mp3 ,并希望在加载HTML页面时播放它。
window.onload = function() {
var file = document.getElementById("thefile");
var audio = document.getElementById("audio");
file.onchange = function() {
var files = this.files;
audio.src = URL.createObjectURL(files[0]);
audio.load();
audio.play();
var context = new AudioContext();
var src = context.createMediaElementSource(audio);
var analyser = context.createAnalyser();
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 256;
var bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
var dataArray = new Uint8Array(bufferLength);
var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var barWidth = (WIDTH / bufferLength) * 2.5;
var barHeight;
var x = 0;
function renderFrame() {
requestAnimationFrame(renderFrame);
x = 0;
analyser.getByteFrequencyData(dataArray);
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
for (var i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
var r = barHeight + (25 * (i / bufferLength));
var g = 250 * (i / bufferLength);
var b = 50;
ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
x += barWidth + 1;
}
}
audio.play();
renderFrame();
};
};
#thefile {
position: fixed;
top: 10px;
left: 10px;
z-index: 100;
}
#canvas {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
audio {
position: fixed;
left: 10px;
bottom: 10px;
width: calc(100% - 20px);
}
<div id="content">
<input type="file" id="thefile" accept="audio/*" />
<canvas id="canvas"></canvas>
<audio id="audio" controls></audio>
</div>
答案 0 :(得分:1)
这些家伙没有什么神圣的-截至2018年12月,Chrome用户开发Web Audio API时已制定了新的自动播放政策。如果AudioContext();
是在用户与手势交互(点击,轻击,打bur,打sn等)之前创建的,则AudioContext();
将被暂停,直到用户这样做为止。
因此,为了适应这种工程奇迹,我添加了一个播放按钮,并将所有内容包装在eventListener中。
src
”“现在的问题是:我不需要像代码一样上载音轨,我只需要一个简单的音轨即可自动播放并删除该上载按钮。假设我有一个track1 .mp3放在同一目录中,并希望在加载我的页面时播放。”
好的,该演示已被适配为加载普通的url,您需要更改此行,使其指向服务器上文件的位置:
audio.src = "https://host.top/path/to/file.mp3";
为certain conflicts with CORS添加了前面提到的行上方的新行:
audio.crossOrigin = "anonymous";
注意:如果此堆栈片段没有声音,请转到此Plunker
document.getElementById('load').addEventListener('click', audioViz);
function audioViz(e) {
var player = document.getElementById("player");
player.crossOrigin = "anonymous";
player.src = "https://glsbx.s3-us-west-1.amazonaws.com/-/dd.mp3";
player.load();
player.play();
var context = new AudioContext();
var src = context.createMediaElementSource(player);
var analyser = context.createAnalyser();
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 256;
var bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
var dataArray = new Uint8Array(bufferLength);
var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var barWidth = (WIDTH / bufferLength) * 2.5;
var barHeight;
var x = 0;
function renderFrame() {
requestAnimationFrame(renderFrame);
x = 0;
analyser.getByteFrequencyData(dataArray);
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
for (var i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
var r = barHeight + (25 * (i / bufferLength));
var g = 250 * (i / bufferLength);
var b = 50;
ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
x += barWidth + 1;
}
}
player.play();
renderFrame();
}
button {
position: fixed;
top: 46px;
left: 46px;
z-index: 100;
display: inline-block;
font-size: 48px;
border: none;
background: none;
color: rgba(223, 6, 39, 0.8);
cursor: pointer;
}
button:hover {
color: rgba(255, 0, 128, 0.8);
}
button:focus {
outline: 0
}
#canvas {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#player {
position: fixed;
left: 10px;
bottom: 10px;
width: calc(100% - 20px);
}
<button id='load' class='load' type='button'>▶</button>
<canvas id="canvas"></canvas>
<audio id="player" controls>
<source src='about:blank'>
</audio>