i am writing a small player that can play from Http. media player can play mp3 from http stream with mPlayer.setDataSource(url); , but i want to save this file. I tried following:
public static void downloadFile(File f, String songUrl, IDownloadListener listener)throws Exception{
URL url = new URL(songUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
InputStream inStream = connection.getInputStream();
FileOutputStream outStream = new FileOutputStream(f);
Integer fileLength = connection.getContentLength();
try {
byte data[] = new byte[16384];
int lastPercentNotify = -1, curPercent;
int count;
int total = 0;
listener.onFileDescriptorAvailable(outStream.getFD()); //!!!! this willnotify about FD
while ((count = inStream.read(data, 0, data.length)) != -1){
total += count;
outStream.write(data, 0, count);
curPercent = (total * 100) / fileLength;
if (curPercent != lastPercentNotify && listener != null && curPercent % 10 == 0){
listener.onDownload(f.getName(), curPercent, 100);
lastPercentNotify = curPercent;
}
}
}finally {
inStream.close();
outStream.close();
}
}
where in my listener i do following:
mPlayer.setDataSource(fileDescr);
//prepare and call mPlayer.start()
But i have various exceptions when i am trying to change the point when i am getting the descriptor.
UPD: My code works fine for saving file. My trouble the playing mp3 and saving in to the sdcard in the same time.