Input WebRTC作为麦克风播放音频输入
我想将我的音频文件作为麦克风输入播放(不发送我的实时语音,但发送我的音频文件)给WebRTC连接的用户。有人能告诉我怎么做吗 我在JS代码中做了以下尝试,例如: 1.base64音频 请帮我解决这个问题。这将是非常值得欣赏的。这里有一个演示,可以帮助您使用chrome播放mp3或wav:Input WebRTC作为麦克风播放音频输入,input,audio-streaming,webrtc,microphone,Input,Audio Streaming,Webrtc,Microphone,我想将我的音频文件作为麦克风输入播放(不发送我的实时语音,但发送我的音频文件)给WebRTC连接的用户。有人能告诉我怎么做吗 我在JS代码中做了以下尝试,例如: 1.base64音频 请帮我解决这个问题。这将是非常值得欣赏的。这里有一个演示,可以帮助您使用chrome播放mp3或wav: 下面是它的写法: 和演示的源代码: 在第三方WebRTC应用程序中使用 更新时间:2014年8月28日星期四下午5:55 以下是如何从服务器获取mp3: function HTTP_GET
更新时间:2014年8月28日星期四下午5:55 以下是如何从服务器获取mp3:
function HTTP_GET(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
if (xhr.status != 200) {
alert("Unexpected status code " + xhr.status + " for " + url);
return false;
}
callback(xhr.response); // return array-buffer
};
}
// invoke above "HTTP_GET" method
// to load mp3 as array-buffer
HTTP_GET('http://domain.com/file.mp3', function(array_buffer) {
// Import callback function that provides PCM audio data decoded as an audio buffer
context.decodeAudioData(array_buffer, function(buffer) {
// Create the sound source
var soundSource = context.createBufferSource();
soundSource.buffer = buffer;
soundSource.start(0, 0 / 1000);
soundSource.connect(gainNode);
var destination = context.createMediaStreamDestination();
soundSource.connect(destination);
createPeerConnection(destination.stream);
});
});
亲爱的Muaz,我已经用
RTCMultiConnection
测试了我的音频文件,它工作得很好,但只是通过将输入作为应该从我的计算机中选择的文件进行传递。我想在上面传递我的服务器文件。为了做到这一点,我尝试了在
中选择一些预定义的文件,但它似乎无法实现。除了FileReader对象之外,还有其他方法可以读取缓冲区吗?感谢百万兄弟:)LiveLongOK,下面是使用Twilio(第三方)API的另一个问题。我把你发给我的代码放进去了,但它不起作用。我想我错过了什么。我需要同时连接RTCMultiConnection和Twilio.Device吗?请引导我。谢谢RTCMultiConnection是一个简单的包装器,因此您不需要使用它。我不知道Twilio API是什么样子,但是上面的代码片段应该在任何允许您在创建对等连接之前传递“自定义流”的应用程序/API中工作。不用担心,我已经将RTCMultiConnection与Twilio集成。但还有另一个问题,即音质传递非常糟糕。没有Twilio,音质很好。我不知道为什么。声音文件的比特率大于256Kbps是否有任何功能或某些必要条件?谢谢
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();
var isPlaying = false;
var sourceNode = null;
var theBuffer = null;
window.onload = function() {
var request = new XMLHttpRequest();
request.open("GET", "sounds/DEMO_positive_resp.wav", true);
request.responseType = "arraybuffer";
request.onload = function() {
audioContext.decodeAudioData( request.response, function(buffer) {
theBuffer = buffer;
} );
}
request.send();
}
function togglePlayback() {
var now = audioContext.currentTime;
if (isPlaying) {
//stop playing and return
sourceNode.stop( now );
sourceNode = null;
analyser = null;
isPlaying = false;
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = window.webkitCancelAnimationFrame;
//window.cancelAnimationFrame( rafID );
return "start";
}
sourceNode = audioContext.createBufferSource();
sourceNode.buffer = theBuffer;
sourceNode.loop = true;
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
sourceNode.connect( analyser );
analyser.connect( audioContext.destination );
sourceNode.start( now );
isPlaying = true;
isLiveInput = true;
return "stop";
}
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var gainNode = context.createGain();
gainNode.connect(context.destination);
// don't play for self
gainNode.gain.value = 0;
document.querySelector('input[type=file]').onchange = function() {
this.disabled = true;
var reader = new FileReader();
reader.onload = (function(e) {
// Import callback function that provides PCM audio data decoded as an audio buffer
context.decodeAudioData(e.target.result, function(buffer) {
// Create the sound source
var soundSource = context.createBufferSource();
soundSource.buffer = buffer;
soundSource.start(0, 0 / 1000);
soundSource.connect(gainNode);
var destination = context.createMediaStreamDestination();
soundSource.connect(destination);
createPeerConnection(destination.stream);
});
});
reader.readAsArrayBuffer(this.files[0]);
};
function createPeerConnection(mp3Stream) {
// you need to place 3rd party WebRTC code here
}
function HTTP_GET(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
if (xhr.status != 200) {
alert("Unexpected status code " + xhr.status + " for " + url);
return false;
}
callback(xhr.response); // return array-buffer
};
}
// invoke above "HTTP_GET" method
// to load mp3 as array-buffer
HTTP_GET('http://domain.com/file.mp3', function(array_buffer) {
// Import callback function that provides PCM audio data decoded as an audio buffer
context.decodeAudioData(array_buffer, function(buffer) {
// Create the sound source
var soundSource = context.createBufferSource();
soundSource.buffer = buffer;
soundSource.start(0, 0 / 1000);
soundSource.connect(gainNode);
var destination = context.createMediaStreamDestination();
soundSource.connect(destination);
createPeerConnection(destination.stream);
});
});