簡體   English   中英

下載的視頻文件無法在Android中播放

[英]Downloaded Video File Can't be Played in Android

問題:
我下載的視頻文件無法從任何播放器播放,更不用說我的應用程序的VideoView,但它可以很好地從URL到VideoView播放。

完成了什么:
我正在下載一個視頻文件,如果它不在外部存儲器中,否則直接從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()使用以下代碼將文件內容作為字符串返回:

@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了。

可能是我遺漏了編解碼器編碼甚至簡單的文件保存部分?

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.
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM