Hi folks, with the help of The Code Project and a Android Developers member I have cobbled together this little shoutcast recorder. I was wondering if you may be able to help me improve it. What I need it to do now is "send" the stream to a socket, I think for mediaplayer to pickup the stream decode the mp3 and play it!! I have read others have done this.
Here's my code it is a bit untidy at the mo, with your help I should finish the service today! [code] package com.jsh.httprequest; /* * Credits must go to : * From The Code Project * ShoutcastStream Class By Dirk Reske | 10 Jun 2007 * http://www.codeproject.com/Articles/19125/ShoutcastStream-Class * * SHOUTcast Stream Ripper By espitech | 14 Aug 2005 * http://www.codeproject.com/Articles/11308/SHOUTcast-Stream-Ripper?msg=2160300#xx2160300xx * Both written in C# * * From http://groups.google.com/group/android-developers/browse_thread/thread/c08b99c0f26fef7d * Android Developers Creating Comunal ShoutCasts Lib by ikalbeniz * * Many thanks go to above for getting me started on this project. It is just a block * of code to grab the metadata from a shoutcast stream at the moment. * * With some of your help, (I hope) it will become functional to enable a radio station to * stream to an Android phone. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; public class HttpRequestActivity extends Activity { private static final String TAG = null; String OutputFile = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // we have to do networking stuff on a diffarant thread! String artist; String title; Thread thread = new Thread() { @Override public void run() { do_my_thing(); } }; thread.start(); } void ParseMetaInfo(String metainfo){ // fileName = Regex.Match(metadataHeader, // "(StreamTitle=')(.*) (';StreamUrl)").Groups[2].Value.Trim(); Pattern pattern = Pattern.compile("(StreamTitle=')(.*) (';StreamUrl)"); Matcher matcher = pattern.matcher(metainfo); } private String MakePath() throws IOException{ String str1 = "00-mar-Recording-" + mydate() + ".mp3"; String str2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MAR/"; File localFile = new File(str2); if (!localFile.exists()){ if (!localFile.mkdirs()) Log.e(TAG, "CreateD dir in sdcard"); } return str2 + str1; } public String mydate(){ Date localDate = Calendar.getInstance().getTime(); return new SimpleDateFormat("dd-MM-yyyy").format(localDate); } private void setOutputFile(String paramString){ OutputFile = paramString; } public void do_my_thing(){ String server = "http://173.192.22.204:8024"; String serverPath ="/"; URLConnection conn = null; URLConnection ServerConn = null; BufferedOutputStream writer = null; int MetaInt = 0; // blocksize of mp3 data // metadata header that contains the actual songtitle String MetaDataHeader = null; // last metadata header, to compare with // new header and find next song String OldMetaDataHeader = null; // create request URL url = null; try { url = new URL("http", "173.192.22.204", 8024,"/"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn = (URLConnection) url.openConnection(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn.setRequestProperty("GET", serverPath + " HTTP/1.0"); conn.setRequestProperty("User-Agent", "WinampMPEG/5.09"); conn.setRequestProperty("Icy-MetaData", "1"); // execute request try { conn.connect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String smetaString = conn.getHeaderField("icy-metaint"); try { MetaInt = Integer.parseInt(smetaString); } catch(NumberFormatException nfe) { System.out.println("Could not parse " + nfe); } //ok then lets get the firstmp3 block final int BUFFER_SIZE = MetaInt; byte[] byBuffer = new byte[BUFFER_SIZE]; try { setOutputFile(MakePath()); writer = new BufferedOutputStream(new FileOutputStream(OutputFile)); BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); // Now read the buffered stream. while (true) { int receivedBytes = in.read(byBuffer, 0, BUFFER_SIZE); //get the block if (receivedBytes < 0){//nothing recived get outa here writer.flush(); writer.close(); // tidy up our recording return; } if ( receivedBytes == MetaInt) { //are at the metaInt int metaLen = in.read(); //read how much metadata there is if (metaLen > 0){ // there is some, get the metadata String metaInfo = null; for (int len=0; len<=(metaLen*16);len++){ int ch = in.read(); metaInfo=metaInfo+ (char)((byte)ch); //wow } //save and send mp3 data MetaDataHeader= metaInfo; if (OldMetaDataHeader != MetaDataHeader) { //flag that song changed ParseMetaInfo(metaInfo); } OldMetaDataHeader = MetaDataHeader; } writer.write(byBuffer, 0, BUFFER_SIZE); //write the mp3 to disk writer.flush(); receivedBytes = 0; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } [/code] -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

