views:

421

answers:

1

Hi, I'm trying to create a sound application, somewhat like Spotify using nAudio open source library. The application is only intended as a personal archive of my own song ideas. Most functionality works great but the WaveChannel32(New WaveFileReader) doesn't support URLs it seems.

Now, the sound files themselves lie in a folder on a server, which is also an SQL Server. I'm not sure on which approach would be the best to connect to the server and get the sound files. How would one actually connect to the server? Would one set up the server as a webserver? Would it be enough to specify the server IP-address?

And finally, how can I create an internet stream which the application can use? There should probably be some sort of buffering of the sound file first, running in one thread. And the actual playback running in a second thread? And how could I do this?

Edit: I've added the code I tried to convert to VB.NET, but I get the error message: Value of type 'System.IO.MemoryStream' cannot be converted to 'String'

The converted code looks like this:

Public Shared Sub PlayMp3FromUrl(ByVal url As String)
        Using ms As New MemoryStream()
            Using stream As Stream = WebRequest.Create(url).GetResponse().GetResponseStream()
                Dim buffer As Byte() = New Byte(32768) {}
                Dim read As Integer = 0
                While (read = stream.Read(buffer, 0, buffer.Length)) > 0
                    ms.Write(buffer, 0, read)
                End While
            End Using

            ms.Position = 0

            Using blockAlignedStream As WaveStream = New BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(New Mp3FileReader(ms)))
                Using waveOut As New WaveOut(0, 500, Nothing)
                    waveOut.Init(blockAlignedStream)
                    waveOut.Play()
                    While (blockAlignedStream.Position < blockAlignedStream.Length)
                        System.Threading.Thread.Sleep(100)
                    End While
                End Using
            End Using
        End Using
    End Sub
+1  A: 

Hi Kenny,

Have a look at this, it contains code on how to Play an MP3 from a URL using NAudio:

http://stackoverflow.com/questions/184683/play-audio-from-a-stream-using-c

Cheers, Sebastian

Sebastian Gray
Ok, I've added some code to the first post, just trying to convert the C# code to VB.NET, but the generators gave me errors. Looks like there's something wrong in the code from that link, atleast it seems to be after converting to VB.NET.I get the error message: "Value of type 'System.IO.MemoryStream' cannot be converted to 'String'"This is at line "New BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(New Mp3FileReader(ms)))"
Kenny Bones
As noted on the NAudio code project site, a version of NAudio post December 2008 is required.
Sebastian Gray