首页 > 解决方案 > 下载的视频无法在 Android 版本 <= 6.1 上播放

问题描述

我有一个视频下载器 android 应用程序它允许人们从 twitter 下载视频,并且在 2 个月内发生了一些变化下载的视频在 Android 版本 <= 6.0 上无法播放错误是:“无法播放此视频”其中一些视频可以播放但大多数不是。相同格式的mp4。

我没有对我的代码进行任何更改。我尝试从浏览器手动下载文件,但仍然出现错误。

// Progress Dialog
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;

// File url to download
private static String file_url = "https://video.twimg.com/ext_tw_video/1122253815884001280/pu/vid/1280x720/xTTWb4wnRMvFzpXk.mp4";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    new DownloadFileFromURL().execute(file_url);

}

/**
 * Showing Dialog
 * */

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case progress_bar_type: // we set this to 0
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Downloading file. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setMax(100);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.setCancelable(true);
        pDialog.show();
        return pDialog;
    default:
        return null;
    }
}

/**
 * Background Async Task to download file
 * */
class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(progress_bar_type);
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            // this will be useful so that you can show a tipical 0-100%
            // progress bar
            int lenghtOfFile = conection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream(),
                    8192);

            // Output stream
            OutputStream output = new FileOutputStream(Environment
                    .getExternalStorageDirectory().toString()
                    + "/2011.mp4");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after the file was downloaded
        dismissDialog(progress_bar_type);

    }

}

我想让这些视频可以像其他人一样播放。一些 mp4 视频是可行的,而大部分不是。我不知道原因是不是编解码器,但我也想让这些可播放。

该视频是情况的示例。

https://video.twimg.com/ext_tw_video/1122253815884001280/pu/vid/1280x720/xTTWb4wnRMvFzpXk.mp4

标签: javaandroidvideomp4

解决方案


High @Level 3.您的示例视频使用Android 版本不支持的H.264 配置文件<= 6
H.264 是视频的“图像”格式(其中音频是 MP3/AAC)。

最低到最高的 Profie 顺序是:基线 --> 主要 --> 高。

请参阅文档:https ://developer.android.com/guide/topics/media/media-formats#video-codecs

媒体信息分析:

Video
ID                             : 1
Format                         : AVC
Format/Info                    : Advanced Video Codec
Format profile                 : High@L3.1

通常,您可以通过提供站点中视频文件的替代编码来进行修复。由于您不负责 Twitter 服务器,因此您必须检查 Twitter 本身是否保留了上传视频的任何 Low/Standard-Def 版本,用于无法处理高清的旧设备。如果找到,则只需为用户提供多个“质量”链接选择。

或者尝试查看 FFmpeg 是否可以播放该格式。在有问题的设备上尝试 VLC Player 应用程序(由 FFmpeg 驱动)。如果播放正常,请尝试将Android-FFmpeg导入您的应用程序代码,您可以使用它在您的应用程序中解码/播放下载的视频。


推荐阅读