【发布时间】:2015-09-29 03:20:20
【问题描述】:
根据我的阅读,我预计以下 JavaScript 代码会记录“一切正常”,但它会遇到错误情况:
var audio = document.createElement('audio');
var ctx = new window.AudioContext();
var source = ctx.createMediaElementSource(audio);
audio.src = 'http://www.mediacollege.com/audio/tone/files/440Hz_44100Hz_16bit_30sec.mp3';
// As @padenot mentioned, this is the number of channels in the source node, not the actual media file
var chans = source.channelCount;
if(chans == 1) {
snippet.log("All is well");
} else {
snippet.log("Expected to see 1 channel, instead saw: " + chans)
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
发生了什么事?
这可能是 CORS 问题吗?有没有其他方法可以确定这个 mp3 文件实际上是单声道的吗?
编辑:正如@padenot 提到的,这是源节点中的频道数,而不是实际的媒体文件
澄清
我希望有一种解决方案可以避免在内存中解码整个音频文件。 decodeAudioData(),根据我的经验,需要一次解码整个 mp3,这可能需要几秒钟。 createMediaElementSource() 允许您在收听时流式传输媒体和解码。
【问题讨论】:
标签: javascript audio web-audio-api