Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / Visual Basic
Article

Read raw SHOUTcast Stream

Rate me:
Please Sign up or sign in to vote.
2.68/5 (7 votes)
25 Jul 2005 54.2K   366   20   5
How to read raw SHOUTcast stream.

Introduction

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

Using the code

VB
'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... :)

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe server committed a protocol violation. Section=ResponseStatusLine Pin
chrsarly044-Oct-07 12:40
chrsarly044-Oct-07 12:40 
AnswerRe: The server committed a protocol violation. Section=ResponseStatusLine Pin
dfrede5-Oct-07 6:28
dfrede5-Oct-07 6:28 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine Pin
Martin Garmendia12-Jan-08 10:51
Martin Garmendia12-Jan-08 10:51 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine Pin
Ducain25-Jun-08 7:59
Ducain25-Jun-08 7:59 
GeneralRe: The server committed a protocol violation. Section=ResponseStatusLine Pin
George Cristian10-May-13 4:44
George Cristian10-May-13 4:44 

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

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