0

我可以从资产文件夹播放视频,但我现在想从服务器播放它。如何更改我的应用程序?

源代码是

  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/..。但我想从服务器获取视频文件并在我的应用程序中播放。

4

1 回答 1

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

您的其余代码应该几乎保持不变。

更多信息请访问:http ://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html

于 2013-04-28T10:31:34.447 回答