Hector
Hector

Reputation: 5418

How to download audio file in Android

I have a published Android application that has an HTTP audio download process.

This processed worked fine until today.

whats wrong with my code?

final URL downloadFileUrl = new URL(mPerformanceSong.getPreviewUrl());
final HttpURLConnection httpURLConnection = (HttpURLConnection) downloadFileUrl.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.connect();

mTrackDownloadFile = new File(RecordPerformance.this.getCacheDir(), "mediafile");
mTrackDownloadFile.createNewFile();
final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
final byte buffer[] = new byte[16 * 1024];

final InputStream inputStream = httpURLConnection.getInputStream();

int len1 = 0;
while ((len1 = inputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, len1);
}

fileOutputStream.flush();
fileOutputStream.close();

The content of the downloaded file appears to be gzip.

does this mean i need to wrap my inputStream in GZIPInputStream?

an example download URL is

http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI

Upvotes: 0

Views: 667

Answers (1)

Sam Dozor
Sam Dozor

Reputation: 40744

I'm actually surprised it's downloading anything - are you sure it is?

The http url that you've posted:

http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI

Is currently redirecting to an https cloudfront address:

https://d28julafmv4ekl.cloudfront.net/...

HttpUrlConnection will not follow redirects across schemes (ie from http to https).

So if you change your *.amazon.com URLs to https, perhaps it would fix your issue...

Upvotes: 1

Related Questions