Search code examples
androidurlbufferrecordingshoutcast

How to record or download stream buffer from shoutcast url in external directory android


I am playing shoutcast url with the help of Exoplayer , i want to save the buffer as .mp3 file format .. i am using this code but this is saving stream very very slow as i am recording stream for 5 min it is only recording stream of about 15-20 sec .. please help me ... thanks in advance for your contribution...

      outputSource.append( "//samplefile.mp3" );
    String os;
   os = outputSource.toString();
   fileOutputStream = new FileOutputStream( os );
 inputStream = new URL( "http://my_url" ).openStream();
while    (true) 
{
int c;
while((c=inputStream.read())!= -1)
{
Log.d(LOG_TAG,"bytesRead="+bytesRead);
fileOutputStream.write(c);
bytesRead++;
}
}

Solution

  • i am recording stream for 5 min it is only recording stream of about 15-20 sec that is because you are reading it byte by byte.
    You need to use a buffer:

    int l;
    byte[] buffer = new byte[1024];
    while ((l = inputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, l);
    }
    

    Also your while(true) cycle is useless