问题
I'm using NAudio 1.7 after i gave up on WaveIn p/invoke... anyway, i'm making a VoIP application and the sample code i found used WaveFileWriter to output to disk, i don't want that, so i used the memoryStream overload instead.
The problem is when i try to play the stream after i stop the recording with the SoundPlayer class, it just doesn't play and continues the code, but if i save it as shown below, i can play it in VLC, but if i try to load it from the file itself, it doesn't play either, this time throwing "Wav header damaged" or something, this throws in the stream if i don't set it to Position 0 too.
Do windows sets the wav file header itself when i save it? Why can VLC play it and SoundPlayer can't even if i save it?
Private waveFile As WaveFileWriter
Private newWave As New NAudio.Wave.WaveIn
Private memstream As New MemoryStream
Public Sub StartRecording()
newWave.WaveFormat = New WaveFormat(44100, 1)
waveFile = New WaveFileWriter(memstream, newWave.WaveFormat)
AddHandler newWave.DataAvailable, AddressOf RecordedDataAvailable
AddHandler newWave.RecordingStopped, AddressOf RecordingStopped
newWave.StartRecording()
End Sub
Private Sub RecordedDataAvailable(sender As Object, e As NAudio.Wave.WaveInEventArgs)
If waveFile IsNot Nothing Then
waveFile.Write(e.Buffer, 0, e.BytesRecorded)
waveFile.Flush()
End If
End Sub
Private Sub RecordingStopped(sender As Object, e As NAudio.Wave.StoppedEventArgs)
Dim fileName As String = Application.StartupPath & "\Test"
File.WriteAllBytes(fileName & ".wav", memstream.GetBuffer)
memstream.Position = 0
Dim player As New SoundPlayer(memstream)
player.Play()
End Sub
回答1:
I know this is C# but I think you get the idea ;) I tried to do same as you did and this works for me.
private WaveFileWriter writer;
private WaveIn waveIn;
private MemoryStream memoryStream;
public MainWindow()
{
InitializeComponent();
waveIn = new WaveIn();
waveIn.DataAvailable += OnWaveInDataAvailable;
waveIn.RecordingStopped += OnWaveInRecordingStopped;
}
private void OnWaveInDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
writer.Write(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
writer.Flush();
}
private void OnWaveInRecordingStopped(object sender, StoppedEventArgs stoppedEventArgs)
{
Save();
}
private void Start()
{
memoryStream = new MemoryStream();
writer = new WaveFileWriter(new IgnoreDisposeStream(memoryStream), waveIn.WaveFormat);
waveIn.StartRecording();
}
private void Stop()
{
waveIn.StopRecording();
}
private void Save()
{
using (var fileStream = File.Create("test.wav"))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
private void Play()
{
var soundPlayer = new SoundPlayer(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
soundPlayer.Play();
}
private void OnRecordClick(object sender, RoutedEventArgs e)
{
Start();
}
private void OnStopClick(object sender, RoutedEventArgs e)
{
Stop();
}
private void OnPlayClick(object sender, RoutedEventArgs e)
{
Play();
}
}
来源:https://stackoverflow.com/questions/27532663/naudio-recording-and-playing-audio