如何录制和播放默认音频设备

时间:2016-10-10 13:58:16

标签: c# wpf naudio

我似乎无法录制默认音频设备的音频,并在其他音频设备上播放。 我不想录制麦克风,但音频设备......

当我播放电影时,我可以通过耳机听到声音,我想将声音复制到我选择的任何音频设备上。

如果您有任何建议,则不必与NAudio合作。

据我所知,NAudio无法做到这一点..

这是我此时用于该任务的代码,但它只接受来自我的麦克风的输入:带有NAudio的代码片段..

void playSoundCopy(int dv0)
{
    disposeWave0();// stop previous sounds before starting
    var waveOut0 = new WaveOut();
    waveOut0.DeviceNumber = dv0;
    wave0 = waveOut0;

    Defaultwave0 = new WaveIn();
    Defaultwave0.DeviceNumber = (int)GetDefaultDevice(Defaultdevice.FriendlyName);
    var waveinReader0 = new WaveInProvider(Defaultwave0);
    wave0.Init(waveinReader0);

    play0 = false;
    Thread.Sleep(1000);

    play0 = true;
    t0 = new Thread(() => { timeline0(); });
    t0.IsBackground = true;
    t0.Start();

    Defaultwave0.StartRecording();
    wave0.Play();
}

真正的问题实际上是我无法从WaveOut设备录制,只有WaveIn ..

工作结果:

void playSoundCopy(int dv0)
{
    disposeWave0();// stop previous sounds before starting
    var waveOut0 = new WaveOut();
    waveOut0.DeviceNumber = dv0;
    wave0 = waveOut0;

    var format0 = Defaultdevice.AudioClient.MixFormat;
    buffer0 = new BufferedWaveProvider(format0);
    wave0.Init(buffer0);

    capture = new WasapiLoopbackCapture(Defaultdevice);
    capture.ShareMode = AudioClientShareMode.Shared;
    capture.DataAvailable += CaptureOnDataAvailable;

    play0 = false;
    Thread.Sleep(1000);

    play0 = true;
    t0 = new Thread(() => { timeline0(); });
    t0.IsBackground = true;
    t0.Start();

    capture.StartRecording();
    wave0.Play();
}
void CaptureOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
     try
     {
         var itm = new byte[waveInEventArgs.BytesRecorded];
         Array.Copy(waveInEventArgs.Buffer, itm, waveInEventArgs.BytesRecorded);
         buffer0.AddSamples(itm, 0, itm.Length);
     }
     catch { }
}

1 个答案:

答案 0 :(得分:1)

您可以使用WasapiLoopbackCapture捕获发送到设备的音频。然后,您可以将其传输到BufferedWaveProvider并使用它来提供另一个输出设备。但是会引入一些延迟,所以不要期望两个设备同步。