[nAudio]异步录制有一些小问题! [英] [nAudio] Async Recording has some minor issues !
本文介绍了[nAudio]异步录制有一些小问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的读者,
我正在使用[NAudio]录制来自我电脑的输入。当我把字节写入Wave文件时,我也播放它,我希望有一种方法可以播放byte [],而不必先写它,但不幸的是没有办法做到这一点。无论如何,这是我的代码,我希望你有一些建议让我的代码更好,因为现在它的工作原理,但它真的很慢。 >。>
Dear Readers,
I am using [NAudio] to record the input from my computer. I also play it while I''ve written the bytes to a Wave file, I hoped there was a way to play the byte[] without having to write it first but unfortunatly there is no way to do that. Anyways, here is my code, and I would hope that you have some suggestions to make my code better because right now it works, but it''s really slow. >.>
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
// -- Create an input (Device/Stream)
WaveIn _InputDevice = new WaveIn();
// -- Change it to the device I want to record it from
// - Using 1 as the default one.
_InputDevice.DeviceNumber = 0;
// -- Change the wave format.
_InputDevice.WaveFormat = new WaveFormat(44100,2);
// -- Create the event handlers.
_InputDevice.DataAvailable += new EventHandler<WaveInEventArgs>(_InputDevice_DataAvailable);
// -- Start recording.
Thread _Thread = new Thread(_InputDevice.StartRecording);
_Thread.Start();
}
WaveFileWriter _Writer;
void _InputDevice_DataAvailable(object sender, WaveInEventArgs e)
{
// -- Get the correct path.
string Path = Application.StartupPath + "\\" + DateTime.Now.TimeOfDay.Seconds + ".wav";
// -- New Writer.
_Writer = new WaveFileWriter(Path, new WaveFormat(24000, 16, 2));
// -- Write all the bytes
_Writer.Write(e.Buffer, 0, e.BytesRecorded);
// -- Flush.
_Writer.Flush();
// -- Close.
_Writer.Close();
// -- Play it with a sound player.
SoundPlayer _Player = new SoundPlayer();
_Player.SoundLocation = Path;
_Player.PlaySync();
// -- Delete it.
File.Delete(_Player.SoundLocation);
}
推荐答案
好的,我自己找到了解决方案。
有一种躺在互联网上的方法,需要一段时间才能找到:
Alright, found the solution myself.
There was a method lying around the internet, took a while to find:
private void Play(byte[] p)
{
try
{
WaveOut ou = new WaveOut();
var s = new MemoryStream(p);
RawSourceWaveStream r = new RawSourceWaveStream(s, new WaveFormat(44100, 2));
ou.Init(r);
ou.Play();
ou.Stop();
ou.Dispose();
s.Dispose();
r.Dispose();
}
catch { }
}
使用BufferedWaveProvider
来执行此操作。在DataAvailable
事件处理程序中,将收到的数据放入BufferedWaveProvider
。然后你可以让一个WaveOut
的实例一直在缓冲的WaveProvider中播放。
Use theBufferedWaveProvider
to do this. In theDataAvailable
event handler, put the data you received into theBufferedWaveProvider
. Then you can have a single instance ofWaveOut
playing from the buffered WaveProvider all the time.
这篇关于[nAudio]异步录制有一些小问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文