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

时间:2016-10-13 11:03:22

标签: android video video-streaming android-videoview

问题:
我下载的视频文件无法从任何播放器播放,更不用说我的应用程序的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();
}

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

@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.81 MB的文件,不能用任何视频播放器作为视频文件打开,更不用说我试过的VideoView了。

可能是我遗漏了编解码器编码甚至简单的文件保存部分?

1 个答案:

答案 0 :(得分:0)

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.
    }
相关问题