如何通过服务器URL替换资产文件夹进行视频播放

时间:2013-04-28 10:10:44

标签: android video-streaming

我可以从资产文件夹中添加视频,但我现在想从服务器播放它。我怎样才能更改我的应用程序?

源代码是

  class DownloadTask extends AsyncTask<String, Void, Object> {
    protected Object doInBackground(String... args) {
        AssetManager am = getAssets();
        String fileName = args[0];
        File file = new File(getExternalFilesDir(null), fileName);
        Log.i("sushi", "Background thread starting");

        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            try {
                InputStream in = am.open("pages/rice/test2.3gp");
                FileOutputStream f = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = in.read(buffer)) > 0) {
                    f.write(buffer, 0, len1);
                }
                f.close();
                in.close();
            } catch (Exception e) {
                Log.d("sushi", e.getMessage());
            }

            if (VideoActivity.this.pd != null) {
                VideoActivity.this.pd.dismiss();
                VideoActivity.this.pd = null;
            }
        }

        return null;
    }

以上代码可以很好地从assets/pages/..文件夹播放视频。但我想从服务器上获取视频文件并在我的应用程序中播放。

1 个答案:

答案 0 :(得分:1)

URL url = new URL("http://www.server.at/file");
InputStream in = url.openStream();

其余代码应该保持不变。

有关详情,请访问:http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html