【问题标题】:How to download audio file in Android如何在 Android 中下载音频文件
【发布时间】:2014-09-26 18:40:57
【问题描述】:

我有一个已发布的具有 HTTP 音频下载过程的 Android 应用程序。

直到今天,这个处理都很好。

我的代码有什么问题?

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();

下载文件的内容似乎是 gzip。

这是否意味着我需要将 inputStream 包装在 GZIPInputStream 中?

一个示例下载 URL 是

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

【问题讨论】:

    标签: android audio gzipinputstream


    【解决方案1】:

    我真的很惊讶它正在下载任何东西 - 你确定它是吗?

    您发布的 http 网址:

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

    当前正在重定向到 https 云端地址:

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

    HttpUrlConnection 不会跟踪跨方案的重定向(即从 http 到 https)。

    因此,如果您将 *.amazon.com 网址更改为 https,也许它会解决您的问题...

    【讨论】:

    • 我绝对确定它曾经有效。现在没有了。
    • 你确定它曾经重定向到不同的方案吗?
    • 不,我不确定发生了什么,直到现在我还没有必要对此进行调查,它正在工作,我很高兴:)。现在我已将原始 URL 中的 http: 更改为 https: 并设置 httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");和 httpURLConnection.setInstanceFollowRedirects(true);它开始工作了。非常感谢您的帮助
    • 好吧,作为测试,我会尝试一个 https url。或您知道不会重定向的网址。
    猜你喜欢
    • 2014-07-29
    • 2019-02-24
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2011-08-07
    相关资源
    最近更新 更多