我在网页上有一个音频元素,我需要添加一个id标签"播放器"对元素。我正在使用getElementById("播放器")。
问题中的元素:
<audio id="player" controls loop>
<source src="Out_of_the_Skies_Under_the_Earth.mp3" type="audio/mp3">
</audio>
我正在使用&#39;播放器&#39;使用Web Audio API标记以使接下来的两行有用:
var mediaElement = document.getElementById('player');
var sourceNode = context.createMediaElementSource(mediaElement);
这是我使用id&#39;播放器的唯一地方,&#39;我对任何其他选择持开放态度。
当我添加id标签时,音频无法在Chrome中播放(它将在没有标签的情况下播放)。它可以在Safari和Opera中播放,但不适用于Chrome。我尝试使用.ogg,使用getElementByClassName将文件弹回到较小的位/采样率,但似乎没有任何工作。
编辑: 此外,我还要注意播放器确实显示了音频文件的正确长度(6:03),它显示进度条移动和正确更新时间。就像声音静音一样。
由于我的音频文件是本地的,因此该片段不一定是我遇到的确切问题。
自发布以来,我注意到我收到错误:&#39; MediaElementAudioSource由于[本地文件]&#39;的CORS访问限制而输出零。我认为使用所需的CORS标头托管我自己的域下的文件可能会解决问题。我现在没有时间实现这一点,但我会在下面的答案中更新我的解决方案。
但与此同时,任何建议都会很棒。
var ctx = window.AudioContext || window.webkitAudioContext;
var context = new ctx();
var mediaElement = document.getElementById('player');
var sourceNode = context.createMediaElementSource(mediaElement);
// create the equalizer. It's a set of biquad Filters
var filters = [];
// Set filters
[60, 170, 350, 1000, 3500, 10000].forEach(function(freq, i) {
var eq = context.createBiquadFilter();
eq.frequency.value = freq;
eq.type = "peaking";
eq.gain.value = 0;
filters.push(eq);
});
// Connect filters in serie
sourceNode.connect(filters[0]);
for(var i = 0; i < filters.length - 1; i++) {
filters[i].connect(filters[i+1]);
}
// connect the last filter to the speakers
filters[filters.length - 1].connect(context.destination);
function changeGain(sliderVal,nbFilter) {
var value = parseFloat(sliderVal);
filters[nbFilter].gain.value = value;
// update output labels
var output = document.querySelector("#gain"+nbFilter);
output.value = value + " dB";
}
&#13;
div audio {
display: block;
margin-bottom:10px;
}
.eq {
margin: 32px;
border:1px solid;
border-radius:15px;
background-color:lightGrey;
padding:10px;
width:300px;
box-shadow: 10px 10px 5px grey;
text-align:center;
font-family: "Open Sans";
font-size: 12px;
}
div.controls:hover {
color:blue;
font-weight:bold;
}
div.controls label {
display: inline-block;
text-align: center;
width: 50px;
}
div.controls label, div.controls input, output {
vertical-align: middle;
padding: 0;
margin: 0;
font-family: "Open Sans",Verdana,Geneva,sans-serif,sans-serif;
font-size: 12px;
}
&#13;
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Equalizer with Bi-Quad Filters</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<html lang="en">
<head>
</head>
<body>
<div class="eq">
<audio id="player" controls crossorigin="anonymous" loop>
<source src="https://vocaroo.com/i/s1lfs67BmoTC">
</audio>
<div class="controls">
<label>60Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 0);"></input>
<output id="gain0">0 dB</output>
</div>
<div class="controls">
<label>170Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 1);"></input>
<output id="gain1">0 dB</output>
</div>
<div class="controls">
<label>350Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 2);"></input>
<output id="gain2">0 dB</output>
</div>
<div class="controls">
<label>1000Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 3);"></input>
<output id="gain3">0 dB</output>
</div>
<div class="controls">
<label>3500Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 4);"></input>
<output id="gain4">0 dB</output>
</div>
<div class="controls">
<label>10000Hz</label>
<input type="range" value="0" step="1" min="-30" max="30" oninput="changeGain(this.value, 5);"></input>
<output id="gain5">0 dB</output>
</div>
</div>
</body>
</html>
<script src="js/index.js"></script>
</body>
</html>
&#13;
答案 0 :(得分:1)
在<source>
元素src
属性中设置的网址不会与CORS标头一起提供,也不是.mp3
文件。
避免
MediaElementAudioSource outputs zeroes due to CORS access restrictions for <URL>
错误,您可以使用fetch()
,Body.blob()
来获取使用Access-Control-Allow-Origin
标头URL.createObjectURL()
投放的资源,以将Blob
转换为{ {1}},然后将Blob URL
元素<audio>
设置为src
。
另请注意Blob URL
标记是自动关闭的;并且应该在全球范围内定义<input>
。
filters
&#13;
var ctx = window.AudioContext || window.webkitAudioContext;
var context = new ctx();
var url = "https://ia600305.us.archive.org/30/items/return_201605/return.mp3";
var filters = [];
fetch(url)
.then(response => response.blob())
.then(blob => {
var blobURL = URL.createObjectURL(blob);
var mediaElement = document.getElementById('player');
mediaElement.src = blobURL;
var sourceNode = context.createMediaElementSource(mediaElement);
// create the equalizer. It's a set of biquad Filters
// Set filters
[60, 170, 350, 1000, 3500, 10000].forEach(function(freq, i) {
var eq = context.createBiquadFilter();
eq.frequency.value = freq;
eq.type = "peaking";
eq.gain.value = 0;
filters.push(eq);
});
// Connect filters in serie
sourceNode.connect(filters[0]);
for (var i = 0; i < filters.length - 1; i++) {
filters[i].connect(filters[i + 1]);
}
// connect the last filter to the speakers
filters[filters.length - 1].connect(context.destination);
});
function changeGain(sliderVal, nbFilter) {
var value = parseFloat(sliderVal);
filters[nbFilter].gain.value = value;
// update output labels
var output = document.querySelector("#gain" + nbFilter);
output.value = value + " dB";
}
&#13;
div audio {
display: block;
margin-bottom: 10px;
}
.eq {
margin: 32px;
border: 1px solid;
border-radius: 15px;
background-color: lightGrey;
padding: 10px;
width: 300px;
box-shadow: 10px 10px 5px grey;
text-align: center;
font-family: "Open Sans";
font-size: 12px;
}
div.controls:hover {
color: blue;
font-weight: bold;
}
div.controls label {
display: inline-block;
text-align: center;
width: 50px;
}
div.controls label,
div.controls input,
output {
vertical-align: middle;
padding: 0;
margin: 0;
font-family: "Open Sans", Verdana, Geneva, sans-serif, sans-serif;
font-size: 12px;
}
&#13;