【发布时间】:2020-09-13 16:53:41
【问题描述】:
我从 Voicemeeter 本地接收数据,它使用 VBAN 协议发送字节流。
我正在尝试使用 NAudio 解码字节并使用 WaveOutEvent 播放音频。我的实现与 NAudioDemo 中的 The NetworkChatDemo 几乎相同,查看此link 以了解有关 NAudio 的更多信息,它是一个很棒的音频库,非常感谢 Mark Heath。
我的实现:
private readonly ILogger<AudioController> _logger;
private NetworkAudioPlayer player;
private readonly INetworkChatCodec _codec;
public AudioController(ILogger<AudioController> logger)
{
_logger = logger;
_codec = new ALawChatCodec();
}
[HttpGet]
[Route("start")]
public IActionResult Start()
{
var receiver = new UdpAudioReceiver(6980);
player = new NetworkAudioPlayer(_codec, receiver);
return Ok("Hello");
}
网络音频播放器:
private readonly INetworkChatCodec codec;
private readonly IAudioReceiver receiver;
private readonly IWavePlayer waveOut;
private readonly BufferedWaveProvider waveProvider;
public NetworkAudioPlayer(INetworkChatCodec codec, IAudioReceiver receiver)
{
this.codec = codec;
this.receiver = receiver;
receiver.OnReceived(OnDataReceived);
waveOut = new WaveOutEvent();
waveProvider = new BufferedWaveProvider(codec.RecordFormat);
waveProvider.BufferDuration = TimeSpan.FromSeconds(300);
waveOut.Init(waveProvider);
waveOut.Play();
}
void OnDataReceived(byte[] compressed)
{
byte[] decoded = codec.Decode(compressed, 0, compressed.Length);
waveProvider.AddSamples(decoded, 0, decoded.Length);
}
public void Dispose()
{
receiver?.Dispose();
waveOut?.Dispose();
}
我在这个实现中遇到了两个问题:
- 首先,我无法将 BufferDuration 设置为无限值,因为它是实时流。
- 其次,我什么都听不到,只有噪音。
如何将 BufferDuration 设置为无限?
如何正确播放音频?
更新:我能够通过将 bufferPorivider 的 DiscardOnBufferOverflow 的值设置为 true 来解决第一个问题。
关于噪音问题,我已经从这个question 找到了解决方案,Mark Heath 提到了
另外,您确定接收到的网络数据包中没有周围的元数据吗?如果是这样,则需要将其剥离,否则会产生噪音。
所以,我在将样本添加到 BufferProvider 时将 offset 的值更改为等于 28,这会导致异常:
void OnDataReceived(byte[] compressed)
{
var l = compressed.Length;
waveProvider.AddSamples(compressed, 28, compressed.Length);
}
堆栈跟踪:
>
> System.ArgumentException
HResult=0x80070057
Message=Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')
Source=System.Private.CoreLib
StackTrace:
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Utils.CircularBuffer.Write(Byte[] data, Int32 offset, Int32 count)
at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count)
at POC.AudioStreaming.AudioManagers.NetworkAudioPlayer.OnDataReceived(Byte[] compressed) in C:\dev\POC.AudioStreaming\POC.AudioStreaming\AudioManagers\NetworkAudioPlayer.cs:line 41
at POC.AudioStreaming.AudioManagers.UdpAudioReceiver.ListenerThread(Object state) in C:\dev\POC.AudioStreaming\POC.AudioStreaming\AudioManagers\UdpAudioReceiver.cs:line 38
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
我该如何解决这个异常?
【问题讨论】: