从Server Android下载视频后丢失音频

时间:2018-08-05 11:15:30

标签: android video downloading chunked-encoding

问题是下载视频后,没有音频。我在Android中的MX-Player和VLC等顶级播放器中打开了视频,但仍然没有音频,但是上传的视频大小和下载的视频大小完全相同。

下载逻辑有什么问题吗? 谁能建议更好的方法从服务器下载分块视频到android中的缓存存储。

响应标题为:

Connection →keep-alive
Transfer-Encoding →chunked

下载视频的代码:

@WorkerThread
public static boolean downloadFile(@NonNull String urlString, @NonNull File outputFile) {
    try {
        URL url = new URL(urlString);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", SharedPref.User.getToken());
        connection.connect();

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return false;
        }

        //noinspection ResultOfMethodCallIgnored
        outputFile.createNewFile();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        DataInputStream stream = new DataInputStream(connection.getInputStream());
        byte[] buffer = new byte[409600];
        int len1;

        while ((len1 = stream.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }

        fos.close();
        stream.close();

        return true;
    } catch (IOException e) {
        //noinspection ResultOfMethodCallIgnored
        outputFile.delete();
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    return false;
}

0 个答案:

没有答案
相关问题