Hi,

Apparently nobody has published a Android Library to play ShoutCast
streams.. there are particular developments but none of the
OpenSource. So why don`t we develop our own library? I will post my
first approach to the class and the community can post modifications
till we get a functional class…

This class makes a HTTP petition to a host and send this query:

GET / HTTP1.0<clrf>
Icy-Metadata:0 <clrf>
<clrf>

an the response is like this:

Recibido: ICY 200 OK
icy-notice1:<BR>This stream requires <a href="http://
www.winamp.com/">Winamp</a><BR>
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux
v1.9.8<BR>

Recibido: icy-name: : radioname
icy-genre:News, Various
icy-url:http://www.sampleur.com
content-type:audio/aacp
icy-pub:1
icy-br:40

<streamdata>

so I split with "icy-br:40" string and i supose the nex content is the
stream data so i save it on a tempfile "tempbuffer.dat".. then the
file should be opened with the mediaPlayer but still don`t know how to
doi it..

As you can see the http response ha to be changed for somethin better
than a split...

************************************************************************************

package dev.andoid.shoutcast;

import java.net.Socket;
import android.app.Activity;
import java.net.InetAddress;
import java.net.Inet4Address;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.net.UnknownHostException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileDescriptor;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class ShoutCastStream extends Activity implements Runnable {

    private Socket clientsocket;
    private String streamUrl;
    private int streamPort;
    private Thread thread;
    public FileDescriptor streamFile;


    public boolean saveStream(String streamUrl, int streamPort) {
        this.streamUrl = streamUrl;
        this.streamPort = streamPort;
        thread = new Thread(this);
        thread.start();
        return true;
    }

    public boolean stopThread(){
        thread.stop();
        return true;
    }

    public void run() {
        try {
        InetAddress server = Inet4Address.getByName(this.streamUrl);

        Socket clientsocket = new Socket(server, this.streamPort);

        BufferedReader entrada = new BufferedReader(new
InputStreamReader( clientsocket.getInputStream() ) );

        // Crea el canal de salida
        PrintWriter salida = new PrintWriter(new OutputStreamWriter
( clientsocket.getOutputStream() ),true );

        // Envía un comando GET al servidor
        salida.println("GET / HTTP1.0\nIcy-Metadata:0\n\n");

        // Se va leyendo el fichero
        String linea = null;
        String aux = "";

        boolean header = false;
        while( (linea = entrada.readLine()) != null ) {
            if(header){
                        WriteSettings(linea);
            }else{
               if(aux.contains("icy-br") ){
                   header = true;
               }else{
                   aux = aux + linea;
               }
            }
        }

          // Se cierra el socket
        clientsocket.close();

        } catch (UnknownHostException e) {
            alertbox("Error", "Server Not Found");
        } catch (IOException e) {
            alertbox("Error", "Couldn't open socket");
        }
    }

    protected void alertbox(String title, String mymessage)
    {
        new AlertDialog.Builder(this)
        .setMessage(mymessage)
        .setTitle(title)
        .setCancelable(true)
        .setNeutralButton(android.R.string.cancel,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int
whichButton){}
        })
        .show();
    }

    public void WriteSettings(String data){
        try{
        FileWriter fWriter = new FileWriter("/sdcard/
tempbuffer.dat",true);
        fWriter.write(data);
        fWriter.flush();
        fWriter.close();
        }catch(Exception e){
            alertbox("Error", "Imposible to save Stream");
        }
     }

}
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to