Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Using NAudio to stream an audio stream

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.
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.
Load the stream in a Memory Stream, convert it into wav and then play it using NAudio
Check the NAudio Examples on their website


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by OneTXxLFeb 10, 2016 11:51 AM 0 272 Feb 10, 2016 11:51 AM
Last Post: OneTXxL
[C#] by ‌‌‌‌‌Thr‌‌‌‌‌Apr 27, 2014 12:29 PM 8 800 Apr 27, 2014 12:29 PM
Last Post: ‌‌‌‌‌Thr‌‌‌‌‌
[C#] by hgdfhsdgdfgdfhJun 30, 2013 12:04 PM 14 1,656 Jun 30, 2013 12:04 PM
Last Post: hgdfhsdgdfgdfh