WebRTC 不适用于 AudioContext [英] WebRTC doesn't work with AudioContext

查看:42
本文介绍了WebRTC 不适用于 AudioContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebRTC 制作音频聊天网站.我有一个问题.

I am making audio chat website using WebRTC. I have one problem.

从对等端接收远程音频时.这不起作用(我听不到任何音频)

When receiving remote audio from peer. This doesn't work (I can't hear any audio)

var audioContext = new AudioContext();
var audioStream = audioContext.createMediaStreamSource(e.stream);
audioStream.connect(audioContext.destination);

虽然这有效

var audio2 = document.querySelector('audio#audio2');
audio2.srcObject = e.stream;

我需要这样做的原因是因为我需要能够控制音频(效果、音量),据我所知,AudioContext 提供了这一点.但由于某种原因,它不起作用.有什么建议吗?

The reason I need to do it is because I need to be able to control the audio (effects, volume), and as I know, AudioContext provides that. But for some reason, it doesn't work. Any suggestions?

谢谢!

推荐答案

使用 .createMediaStreamSource().createGain()

var ctx = new AudioContext();
var source = ctx.createMediaStreamSource(stream);
var gainNode = ctx.createGain();
gainNode.gain.value = .5; 
source.connect(gainNode);
source.connect(ctx.destination);

jsfiddle https://jsfiddle.net/tkw13bfg/2

或者,创建一个 AudioNode,使用 .createGain()

Alternatively, create an AudioNode, use .createGain()

var ctx = new AudioContext();
var audio = new Audio();
audio.srcObject = stream;
var gainNode = ctx.createGain();
gainNode.gain.value = .5;   
audio.onloadedmetadata = function() {
  var source = ctx.createMediaStreamSource(audio.srcObject);
  audio.play();
  audio.muted = true;
  source.connect(gainNode);
  gainNode.connect(ctx.destination);
}

这篇关于WebRTC 不适用于 AudioContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆