Hack Forums
[C#] Using NAudio to stream an audio stream - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Using NAudio to stream an audio stream (/showthread.php?tid=5104059)



Using NAudio to stream an audio stream - YashiroUchiha - Dec 22, 2015

I have an audio stream through a URL and I need to be able to play it preferably with NAudio but I cant seem to get it to work or find anyone else who has tried doing what I need. I don't want to save the audio stream as a file I want to get the audio stream I have and send the bytes elsewhere.

This is what I have so far:

Code
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(e.Args[0]);
VideoInfo video = videoInfos.Where(info => info.CanExtractAudio).OrderByDescending(info => info.AudioBitrate).First();
Console.WriteLine(video.Title + " Bitrate: " + video.AudioBitrate);
using (Stream stream = WebRequest.Create(video.DownloadUrl).
                                              GetResponse().
                                        GetResponseStream())
      {
          var outFormat = new WaveFormat(48000, 16, 1);
          int blockSize = outFormat.AverageBytesPerSecond / 5;
          byte[] buffer = new byte[blockSize];
          int read;
          while((read = stream.Read(buffer, 0, buffer.Length)) > 0)
               {
                    voice.SendVoicePCM(buffer, read);
               }
     }

When run it just plays a bunch of static.


* - He B TeMy - Dec 22, 2015

I don't have time to read and update the code i got for you, but, this used to work in naudio 1.6 if i recall correctly (the last version that supported .net 2.0) .

Code
Public Sub StartCapture(values As String)
        Dim splitted() As String = values.Split("|"c)
        _waveIn = New WaveIn()

        _waveIn.WaveFormat = New WaveFormat(CInt(splitted(0)), CInt(splitted(1)), CInt(splitted(2)))

        AddHandler _waveIn.DataAvailable, AddressOf DataAvailable
        AddHandler _waveIn.RecordingStopped, AddressOf RecordingStopped

        _waveIn.StartRecording()

        SendPacket(_client, _packer, DirectCast(PacketHeader.SoundRecorder, Byte), DirectCast(SoundRecorder.AverageTransferRate, Byte), _waveIn.WaveFormat.AverageBytesPerSecond)
        IsRecording = True
    End Sub

    Public Sub StopCapture()
        If _waveIn IsNot Nothing Then
            _waveIn.StopRecording()
        End If

        IsRecording = False
    End Sub

Private Sub RecordingStopped(sender As Object, e As EventArgs)
        If _waveIn IsNot Nothing Then
            _waveIn.Dispose()
        End If

        IsRecording = False
    End Sub

    Private Sub DataAvailable(sender As Object, e As WaveInEventArgs)
        SendPacket(_client, _packer, DirectCast(PacketHeader.SoundRecorder, Byte), DirectCast(SoundRecorder.StartCapture, Byte), e.Buffer, e.BytesRecorded)
    End Sub

Hope this helps, 'SendPacket' is explicit in the name i think you know what it does.
I just copied this from a previous project of mine, i don't have a c# version of this, but should be straightforward... adapt as you want.


* - limit3l3ss - Jan 3, 2016

Load the stream in a Memory Stream, convert it into wav and then play it using NAudio
Check the NAudio Examples on their website