Click here to Skip to main content
Click here to Skip to main content

Read raw SHOUTcast Stream

, 25 Jul 2005
Rate this:
Please Sign up or sign in to vote.
How to read raw SHOUTcast stream.

Introduction

This is only a raw solution to grab a SHOUTcast stream from a server.

Using the code

'Imports System.Net
'Imports System.Text

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    SHOUTcast_Save("http://192.168.168.97:80/stream/2001", "c:\song_raw.mp3")
End Sub

Public Function SHOUTcast_Save(ByVal sURL As String, _
                ByVal sFileName As String) As Boolean

    Dim webreq As System.Net.HttpWebRequest = _
                  CType(System.Net.WebRequest.Create(sURL), _
                  System.Net.HttpWebRequest)

    webreq.Headers.Clear()
    webreq.Headers.Add("GET", "/stream/2001 HTTP/1.0")
    webreq.UserAgent = "WinampMPEG/5.09"
    webreq.Headers.Add("Icy-MetaData", "1")

    Dim webres As System.Net.WebResponse = webreq.GetResponse()

    Dim oReader As System.IO.Stream = webres.GetResponseStream()
    Dim oFile As System.IO.Stream = New System.IO.FileStream(sFileName, _
                 System.IO.FileMode.OpenOrCreate, _
                 System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)

    For Each oHeader As System.Net.WebHeaderCollection In webres.Headers

        '(0)    "icy-notice1"    String
        '(1)    "icy-notice2"    String
        '(2)    "icy-name"       String
        '(3)    "icy-genre"      String
        '(4)    "icy-url"        String
        '(5)    "icy-pub"        String
        '(6)    "icy-metaint"    String  'how often the metadata 
        '                                    is sent in the stream
        '(7)    "icy-br"         String
        '(8)    "icy-irc"        String
        '(9)    "icy-icq"        String
        '(10)    "icy-aim"       String

        'see http://ample.sourceforge.net/developers.shtml
        'icy-notice1 - An informational message. 
        'icy-notice2 - Another informational message, 
        '              probably "icy-notice3", "icy-notice4" etc.
        '              can also be used. 
        'icy-name - The name of the stream that the server is sending,
        '           this is usually displayed along with
        '           the current song title in clients. 
        'icy-genre - The genre of the music served. 
        'icy-url - An URL associated with the stream, 
        '          usually the homepage of the "webradio" or similar. 
        'icy-pub - Not sure, believe it indicates 
        '          if the stream is public or private 
        'icy-br - BitRate, seems mostly informational as most
        '         clients encountered seem to support VBR (Variable BitRate). 
    Next

    Dim b As Integer
    While True
        b = oReader.ReadByte()
        If b = -1 Then Exit While
        oFile.WriteByte(Convert.ToByte(b))

        'ToDo: Get the metadata (Thanks to SmackFU 
        'http://www.smackfu.com/stuff/programming/)
        'Read the data stream as you normally would, 
        'keeping a byte count as you go. 
        'When the number of bytes equals the metadata 
        'interval, you will get a metadata block.
        'The first part of the block is a length specifier, 
        'which is the next byte in the stream. 
        'This byte will equal the metadata length / 16. 
        'Multiply by 16 to get the actual metadata length. 
        '(Max byte size = 255 so metadata max length = 4080.) 
        'Now read that many bytes and you will 
        'have a string containing the metadata. 
        'Restart your byte count, and repeat. Yay!
        'Note that the metadata length is set to 0 most 
        'of the time, meaning there is no metadata. 
        'The metadata is normally sent at two particular 
        'places: immediately after connecting and when 
        'the song changes. It seems as if some servers 
        'aren't real diligent about the former, 
        'so it might take a while to get your first block.
        'Also, be sure you don't count the bytes in the metadata 
        'length field or the metadata when you're figuring 
        'out whether you've hit the interval. Only the MP3 
        'data counts. So you can't be lazy and just keep 
        'a total byte count and mod it.

        'ToDo: Interpret the metadata
        'Part of the metadata string should look like this:
        'StreamTitle='title of the song';

    End While

    oReader.Close()
    oFile.Close()

End Function
//

Points of Interest

You see, that the metadata is not parsed. (This means you must not write all bytes.) Good Luck... Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Dani Forward

United States United States
No Biography provided

Comments and Discussions

 
QuestionThe server committed a protocol violation. Section=ResponseStatusLine Pinmemberchrsarly044-Oct-07 12:40 
AnswerRe: The server committed a protocol violation. Section=ResponseStatusLine Pinmemberdfrede5-Oct-07 6:28 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine PinmemberMartin Garmendia12-Jan-08 10:51 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine PinmemberDucain25-Jun-08 7:59 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine PinmemberGeorge Cristian10-May-13 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 26 Jul 2005
Article Copyright 2005 by Dani Forward
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid