download audio files

All your problems with Audio, Video and Images.

download audio files

Postby valibab » Fri Jan 09, 2009 9:14 am

hi,
sorry 4 that i have posted twice the same query.

can anybody help with code to download audio files and save to local file

regards.
valibab
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 03, 2009 7:11 am

Top

Postby abel910 » Thu Jan 15, 2009 7:27 am

can you describe your problem clearly? Maybe i can help you .
abel910
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 12, 2009 5:59 am

Postby MrSnowflake » Thu Jan 15, 2009 2:57 pm

You can download audio files just as you download any binary file with java...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby valibab » Tue Jan 20, 2009 4:02 pm

i want to download audio file from an url and save it to local file
can u help me how i can do this
valibab
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 03, 2009 7:11 am

example code

Postby ryderster » Fri Feb 13, 2009 4:16 pm

Hi lads,
below is an example that works for me. The function takes ina url of a mp3 file.
it then stores the file on the emulator in the /data/data/yourapplication/


Not sure if is the best way to do it but it works for me

private String getMp3File(String path) throws IOException {

URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();


InputStream stream = cn.getInputStream();
String contentType = cn.getContentType();
int contentLength = cn.getContentLength();
if (stream == null)
throw new RuntimeException("stream is null");


InputStream in = new BufferedInputStream(stream);
byte[] data = new byte[contentLength];

int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
in.close();


if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}


String filename = url.getFile().toString();
filename=filename.substring(filename.lastIndexOf('/') + 1);
FileOutputStream out = openFileOutput(filename, MODE_WORLD_READABLE);
out.write(data);
out.flush();
out.close();

cn=null;

return filename;
}
ryderster
Freshman
Freshman
 
Posts: 2
Joined: Fri Feb 13, 2009 4:05 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 41 guests