无法在 Android 中播放下载的视频文件

Downloaded Video File Can't be Played in Android

问题:
我下载的视频文件无法从任何播放器播放,更不用说我应用程序的 VideoView,但它可以很好地从 URL 播放到 VideoView。

完成的内容:
如果它不在外部存储中,我正在下载 a video file,否则直接从 URL 播放到 VideoView。
VideoView部分代码如下:

final VideoView vvPlayer = (VideoView) findViewById(R.id.vvPlayer);
MediaController mc = new MediaController(MainActivity.this);
mc.setAnchorView(vvPlayer);
vvPlayer.setMediaController(mc);

if (videoFile.exists()) {
    // vvPlayer.setVideoURI(Uri.fromFile(videoFile));  <-- Also Tried :(
    vvPlayer.setVideoPath(videoFile.getAbsolutePath());
    Toast.makeText(this, "Playing from local ...", Toast.LENGTH_SHORT).show();
} else {
    vvPlayer.setVideoPath(VIDEO_PATH);
    Toast.makeText(this, "Playing online & caching ...", Toast.LENGTH_SHORT).show();
    downloadVideoFile();
}

核心部分,即 downloadVideoFile() 方法的 AsyncTask the doInBackground() returns 使用以下代码将文件内容作为字符串:

@Override
protected String doInBackground(Void... params) {

    URLConnection conn;
    try {
        URL httpFileUrl = new URL(fileUrl);
        conn = httpFileUrl.openConnection();
        conn.connect();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    Log.d(TAG, "Connection opened");
    InputStream inputStream;
    try {
        inputStream = new BufferedInputStream(conn.getInputStream(), 4096);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    BufferedReader bufferedReader = new BufferedReader(
                                 new InputStreamReader(inputStream));

    int responseLen = 0;
    StringBuilder stringBuilder = new StringBuilder();
    String responseStr;
    try {
        while ((responseStr = bufferedReader.readLine()) != null) {
            // Log.i(TAG, "Response read: " + responseStr);
            stringBuilder.append(responseStr.trim());
            // ...my progress-bar related codes
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    responseStr = stringBuilder.toString();
    return responseStr;
}

获取文件内容后,我使用以下代码将它们简单地保存在文件中:

        try {
            if (!APP_DIRECTORY.exists())
                APP_DIRECTORY.mkdirs();
            if (videoFile.createNewFile())
                Log.d(TAG, "Vide-file newly created");

            FileOutputStream fos = new FileOutputStream(videoFile);
            fos.write(fileContent.getBytes());
            fos.flush();
            fos.close();
        } catch (IOException e) {
            Log.d(TAG, "Exception for new creation of videoFile");
            e.printStackTrace();
        }

最终结果是一个8.81MB的文件,用任何视频播放器都无法打开,更不用说我试过了VideoView.

可能是我遗漏了 codecencoding 甚至简单的 file-saving[=37] =]部分?

Replace your doInBackground with this:

@Override
protected String doInBackground(Void... params) {

    URLConnection conn;
    try {
        URL httpFileUrl = new URL(fileUrl);
        conn = httpFileUrl.openConnection();
        conn.connect();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    Log.d(TAG, "Connection opened");
    InputStream inputStream;
    try {
        inputStream = new BufferedInputStream(httpFileUrl.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp4");

byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
            inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }


    return null;
}


And in onPostExecute,

@Override
    protected void onPostExecute(String file_url) {

        String videoPath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.mp4";
        // Now pass this path for playing video.
    }