【发布时间】:2014-04-21 12:37:53
【问题描述】:
我有一个嵌入了 soundcloud 元素的小网页,我想做一个频谱分析仪。到目前为止,我已经成功地制作了一个只使用本地声音文件的文件,但我不知道如何从嵌入的元素中获取音频数据。
有没有办法从嵌入的元素中提取声音数据?
如果这不是一项简单的任务,那么如果可能的话,能够分析当前正在浏览器中播放的声音同样令人满意。
提前致谢:)
代码(不是我写的,大部分都是从教程中找到的)
HTML(嵌入式 soundcloud 标签)
<iframe id="em1" width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/144737157&auto_play=false&hide_related=false&visual=true"></iframe>
JS,我希望以某种方式将 SoundCloudAudioSource 附加到嵌入式标签或整个浏览器窗口。
var SoundCloudAudioSource = function(audioElement) {
var player = document.getElementById(audioElement);
var self = this;
var analyser;
var audioCtx = new (window.AudioContext || window.webkitAudioContext); // this is because it's not been standardised accross browsers yet.
analyser = audioCtx.createAnalyser();
analyser.fftSize = 256; // see - there is that 'fft' thing.
var source = audioCtx.createMediaElementSource(player); // this is where we hook up the <audio> element
source.connect(analyser);
analyser.connect(audioCtx.destination);
var sampleAudioStream = function() {
// This closure is where the magic happens. Because it gets called with setInterval below, it continuously samples the audio data
// and updates the streamData and volume properties. This the SoundCouldAudioSource function can be passed to a visualization routine and
// continue to give real-time data on the audio stream.
analyser.getByteFrequencyData(self.streamData);
// calculate an overall volume value
var total = 0;
for (var i = 0; i < 80; i++) { // get the volume from the first 80 bins, else it gets too loud with treble
total += self.streamData[i];
}
self.volume = total;
};
setInterval(sampleAudioStream, 20); //
// public properties and methods
this.volume = 0;
this.streamData = new Uint8Array(128); // This just means we will have 128 "bins" (always half the analyzer.fftsize value), each containing a number between 0 and 255.
this.playStream = function(streamUrl) {
// get the input stream from the audio element
player.setAttribute('src', streamUrl);
player.play();
}
};
【问题讨论】:
-
分享一些代码。您使用小部件还是 scoembed 方法?一般来说 - mp3 数据位于被转发到亚马逊的 url 后面。
-
我可以从嵌入元素访问这个 mp3 数据吗?
-
我对 sc 嵌入的东西不是很熟悉,但你已经有了这些信息 (api.soundcloud.com/tracks/144737157) - 你可以使用他们的 api 来获取 mp3 文件。
-
你应该看看这个答案:stackoverflow.com/questions/13455956/…
标签: javascript audio soundcloud spectrum