c# - 使用回调从WCF发送混合声音

标签 c# wcf audio-streaming naudio audio

我想将混合声音从WCF服务器传输到所有连接的客户端。为此使用WCF服务回调。使用naudio库混合声音。

这是服务器端(WCF方法)的小示例:

MixingSampleProvider _mixer = new MixingSampleProvider(sound32.WaveFormat);
SampleToWaveProvider _sampleToWave = new SampleToWaveProvider(_mixer);

// service method
byte[] buffer = new byte[1000];
do{
     _sampleToWave.Read(buffer, 0, 1000);
     client.Callback.SendBuffer(buffer);
} while (_isPlaying)

和客户端:
BufferedWaveProvider _bufferedWave = new BufferedWaveProvider(sound32.WaveFormat);
// DirectSoundOut _output = new DirectSoundOut();
WaveOut _output = new WaveOut();

_output.Init(_bufferedWave);

// callback event method
if (_output.PlaybackState != PlaybackState.Playing)
    _bufferedWave.AddSamples(buffer, 0, 1000);

// now in timer_tick event method
// if(_bufferedWave.BufferedDuration.TotalSeconds > 0.5)
//    _output.Play();
// else
//    _output.Pause();

我是新手,所以我有几个问题。
这个主意好吗?有没有更简单的选择来解决这个问题?

[EDIT_1]我使用本地两种方法创建了测试应用程序,应该对此进行模拟,然后我发现_bufferedWave.BufferedBytes在播放缓冲声音时不会清除(并且会立即溢出)。有人可以告诉我,为什么?

[EDIT_1]将_output字段的类型从DirectSoundOut更改为WaveOut,这很有帮助。
我所做的第二个更改是,我添加了DispatcherTimer来处理缓冲持续时间大于0.5的情况(根据naudio MP3Streaming示例)。

现在,我在争取缓冲时间。我只能在_timer_Tick事件方法中听到一段时间的声音:
_bufferedWave.BufferedDuration.TotalSeconds > XX // this XX is time I can hear sound

有什么想法或意见吗?

最佳答案

我不确定这是否会按您希望的方式工作。 WCF是基于TCP的,并且TCP不用于广播音频视频或由于需要不断检查数据包而需要速度的任何内容(例如游戏)。

我以前曾使用过naudio通过网络将声音传输到侦听器客户端,但是要使其正常工作,您将需要使用UDP。

如果您想让它在互联网上工作,则需要研究UDP穿孔。

同样,在传输音频之前,您应该使用ALaw或MuLaw解码器/编码器将其从16bit压缩到8bit,然后在接收到音频后再压缩回16bit。

关于c# - 使用回调从WCF发送混合声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27101449/

相关文章:

c# - AvalonDock : When using DocumentPane binding, 新 DocumentContent 项目的选项卡不可单击

c# - WCF 流 : Framing mode error in net. 仅 tcp

c# - Windows App Certification Kit 错误(HighVersionLie 失败)

c# - 远程机器中的日期文化问题

.net - 声明 ClientBase 期间设备崩溃

ffmpeg - 从 IP 摄像机/rtsp 到网站的实时音频流

Android - 从立体声 .mp3 中提取一个 channel ,通过 AudioStream 的两个 channel 应用它

ios - 带有定时元数据 "in the wild"的 HTTP 实时流式传输 (HLS) 广播电台音频流

c# - 如何从单元测试中获取项目文件夹中的路径

c# - 加载 View 后是否可以在 Controller 中调用操作?