Page 1 of 1

[windows] streaming mp3 for winamp sample

Posted: Fri Aug 23, 2002 5:39 am
by BackupUser
Code updated for 5.20+
note syntax tested only
Restored from previous forum. Originally posted by ricardo.

This is some snipet that acts like a ShotCast server (the one that streams mp3s for winamp).
As there is no open-protocol, i figure it out by myself and could not work properly in some winamp versions (i dont know).

If someone could figure some changes let me know please .

Thanks.

Code: Select all

InitNetwork()
Buffer = AllocateMemory(1024)
myvar.s = Space(3000)
Result = CreateNetworkServer(0,7777)
MyMp3File.s = "u2.mp3" ; your mp3 file

If OpenWindow(0,1,1,250,20,"ShoutCast Server",#PB_Window_SystemMenu)
  
  ButtonGadget(0,0,0,250,20,"Stop")
  Repeat
    EventID=WaitWindowEvent(30)
    ServerEvent = NetworkServerEvent()
    ClientID = EventClient() 
    If ServerEvent
      Select ServerEvent
        Case 2
          ReceiveNetworkData(ClientID,@myvar,Len(myvar))
          ;Header receive from Winamp when trying to connect
          ;GET / HTTP/1.0
          ;Host: 127.0.0.1
          ;User-Agent: WinampMPEG/2.7
          ;Accept: */*
          ;Icy-MetaData:1
          ;Connection: close
          If FindString(myvar,"GET / HTTP/1.0",0)
            ;Respond to connect try with this header redirecting to the music stream
            msg$ = "HTTP/1.0 302 Found" + Chr(13) + Chr(10)
            msg$ = msg$ + "Content-type:text/html" + Chr(13) + Chr(10)
            msg$ = msg$ + "Location: [url]http://127.0.0.1:7777/stream/1003[/url]" + Chr(13) + Chr(10)
            msg$ = msg$ + Chr(13) + Chr(10)
            SendNetworkString(ClientID,msg$)
            SetWindowText_(WindowID(0),"ClientConnected")
          ElseIf FindString(myvar,"GET /stream/1003 HTTP/1.0",0)
            ;Header sended with the music
            msg$ = "ICY 200 OK" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-notice1: This stream requires Winamp[/url]" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-notice2: SHOUTcast Distributed Network Audio Server/SolarisSparc v1.8.12" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-name: PureBasic ShoutCast Server!" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-genre: All" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-url: [url]http://www.purebasic.com[/url]" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-pub: 1" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-metaint: 8192" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-br: 128" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-irc: #shoutcast" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-icq: 0" + Chr(13) + Chr(10)
            msg$ = msg$ +"icy-aim: N/A" + Chr(13) + Chr(10)
            msg$ = msg$ + Chr(13) + Chr(10)  
            SendNetworkString(ClientID,msg$) 
            SetWindowText_(WindowID(0),"Streaming!!")
            OpenFile(0,MyMp3File)
            While Eof(0) = 0
              Delay(1)
              WindowEvent()
              FileSeek(0,Cont * 1024)
              ReadData(0,Buffer,1024)
              SendNetworkData(ClientID,Buffer,1024)
              Cont = Cont + 1
              WindowEvent()
              If StopIt
                SetWindowText_(WindowID(0),"Stoped")
                Break
              EndIf
            Wend
            SetWindowText_(WindowID(0),"Finished!!")
            
            CloseFile(0)
            
            
          EndIf
      EndSelect
      
      
    EndIf
    
    Select EventID
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            StopIt = 1
            
        EndSelect
        
    EndSelect
    
  Until EventID=#PB_Event_CloseWindow
EndIf
End
But how make it works?
Ok, create a m3u file with the next text:

#EXTM3U
http://127.0.0.1:7777

and the just doubleclick on it (of course if you have winamp installed!!).
To create it just open notepad paste the text and goto save as and in then write inside quotes "test.m3u" and save it, then try...

Thanks

Posted: Sun Aug 25, 2002 1:24 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

Hi ricardo
I am not sure.. maybe i make any mistake..!? But i cant get work your example to listen to mp3 stream...

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten

Posted: Sun Aug 25, 2002 5:17 pm
by BackupUser
Restored from previous forum. Originally posted by ricardo.

Hi,

1.- You need to have Winamp installed
2.- Substitute the name of the song on the code with some valid path/song on your PC.
3.- Run the server
4.- Create the m3u (playlist) described before
5.- DoubleClick on it

Then it should works