我尝试将一些音频(在录制点击按钮之后)录制到MemoryStream中,然后使用NAudio在visual studio(WPF)中播放。我生成了一个RawSourceWaveStream,它在单击播放按钮时获取myMemoryStream,但我认为这是在窃听代码。 它没有播放......这是我的代码 我哪里出错?
namespace RecAndPlay
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private WaveIn myWaveIn=null;
private MemoryStream myMemoryStream = null;
private WaveOut myWaveOut= null;
private RawSourceWaveStream myRaw = null;
private void myRecord(object sender, RoutedEventArgs e)
{
{
RecordButton.Content = "Stop Rec";
myWaveIn = new WaveIn();
myWaveIn.WaveFormat = new WaveFormat(44100, 1);
myWaveIn.DeviceNumber = 0;
myWaveIn.DataAvailable += myWaveIn_DataAvailable;
myMemoryStream = new MemoryStream();
myWaveIn.StartRecording();
}
}
void myWaveIn_DataAvailable(object sender, WaveInEventArgs e)
{
myMemoryStream.Write(e.Buffer, 0, e.BytesRecorded);
}
private void myPlay(object sender, RoutedEventArgs e)
{
myWaveOut = new WaveOut();
myRaw = new RawSourceWaveStream(myMemoryStream, myWaveIn.WaveFormat);
myWaveOut.Init(myRaw);
myWaveOut.Play();
}
private void mtStopRec(object sender, RoutedEventArgs e)
{
myWaveIn.StopRecording();
RecordButton.Content = "Record";
}
}
}
答案 0 :(得分:3)
在播放音频之前,您需要将Position
上的MemoryStream
设置为0。另外,请记住,使用此解决方案时,您不应该在录制时尝试播放。