从服务器下载后,Thr Video无法播放

时间:2018-07-10 05:12:07

标签: android android-videoview

我正在使用视频视图,并想通过单击“播放”按钮从服务器下载视频,下载后将自动播放,但无法播放。它已下载但无法播放。

这是我当前的播放代码:

void downloadFile() {
        try {
            AudioURL = "http://" + Const.pathSelected;
            URL url = new URL(AudioURL);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();
            //set the path where we want to save the file
            File SDCardRoot = Environment.getExternalStorageDirectory();
            //create a new file, to save the downloaded file
            File file = new File(SDCardRoot, "/Story/");
            if (!file.exists()) {
                file.mkdir();
            }

            String[] pathname = Const.pathSelected.split("/");
            fileName = pathname[3];
            OutputStream fileOutput = new FileOutputStream(new File(file, fileName));
            Const.localPath = file.getPath() + "/" + fileName;
            //Stream used for reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();
            .......
            //close the output stream when complete //
            fileOutput.close();


            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("su -c am startservice -n com.android.systemui/.SystemUIService");
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                   uri = Uri.parse(Const.localPath);
            //        uri = Uri.fromFile(new File(Const.localPath));

                videoView.setMediaController(mediaController);
                videoView.setVideoURI(uri);
                videoView.requestFocus();
                videoView.start();

            } catch (Exception e) {
                e.printStackTrace();
            }

}

1 个答案:

答案 0 :(得分:1)

也许您正在尝试在下载完成之前播放视频,在这种情况下将无法播放,请尝试下面的代码下载视频(此处我正在传递URL的ArrayList并更新进度,因为我一次下载了多个视频)

class DownloadTask extends AsyncTask<ArrayList<String>, String, Void>
{
    String file_name = "";
    String total_urls = "";
    String current_no ="";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(ArrayList<String>... passed_url)
    {
        ArrayList<String> al_url = new ArrayList<String>();
        al_url = passed_url[0];
        total_urls = ""+al_url.size();
        for (int s = 0; s < al_url.size(); s++) {
            int count = 0;
            try {
                //URL url = new URL(f_url[0]);
                URL url = new URL(al_url.get(s));
                URLConnection connection = url.openConnection();
                connection.connect();

                StringTokenizer tokenizer = new StringTokenizer("" + al_url.get(s), "/");
                file_name = "";
                current_no = ""+(s+1);
                while (tokenizer.hasMoreTokens()) {
                    file_name = tokenizer.nextToken();
                }


                int lenght_of_file = connection.getContentLength();

                File outputFile = new File(Utilities.ADS_DIR_PATH, file_name);

                InputStream input = new BufferedInputStream(url.openStream(), 1024);
                OutputStream output = new FileOutputStream(outputFile);

                byte data[] = new byte[1024];
                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenght_of_file));

                    output.write(data, 0, count);

                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                //Toast.makeText(DownloadTask.this, ""+e, Toast.LENGTH_SHORT).show();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        Intent display_intent = new Intent(SplashActivity.this, NewDisplay.class);
        display_intent.putExtra("LOC_NAME", loc_name);
        startActivity(display_intent);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        //super.onProgressUpdate(values);
        progressBar.setProgress(Integer.parseInt(values[0]));
        txtv_desc.setText("Downloading"+" ("+current_no+"/"+total_urls+")... "+ file_name + "     " + values[0] + "%");
    }
}

并使用

调用
DownloadTask task = new DownloadTask();
task.execute(al_videoURLs);