![](/img/trans.png)
[英]Browser/tab close detection using javascript (or any other language)
[英]Is there a way to capture a browser's audio using Javascript or any other web language?
我想創建一個顯示交互式視覺效果的簡單網站,我希望其中一些是音頻驅動的。 我希望訪客能夠用他們自己選擇的音樂來驅動視覺效果。 除了從麥克風輸入音頻輸入之外,我很難找到其他任何文檔。
例如,我的網頁(在標簽1中)運行javascript代碼X,允許我處理在Web瀏覽器的另一個選項卡中播放的音頻流。 這可能嗎?
您可以使用WebRtc API獲取麥克風流
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio:true}, success, function(e) {
});
}
...............
function success(e) {
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
// the sample rate is in context.sampleRate
audioInput = context.createMediaStreamSource(e);
var bufferSize = 2048;
recorder = context.createScriptProcessor(bufferSize, 1, 1);
recorder.onaudioprocess = function(e){
console.log ('recording');
var left = e.inputBuffer.getChannelData(0);
// stream
window.Stream.write(convertoFloat32ToInt16(left));
}
audioInput.connect(recorder)
recorder.connect(context.destination);
function convertoFloat32ToInt16(buffer) {
var l = buffer.length;
var buf = new Int16Array(l)
while (l--) {
buf[l] = buffer[l]*0xFFFF; //convert to 16 bit
}
return buf.buffer
}
如果您只想要音頻流的可視化,您可以使用javascript插件,例如在此處輸入鏈接描述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.