从网址下载SDCard上的视频 [英] Download Video on SDCard from url

查看:199
本文介绍了从网址下载SDCard上的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请从网址下载sdcard上的视频。我提到这个链接。但是这个链接包含了.mp4格式的URL。我必须下载新闻Feed视频。我的格式不是.mp4格式。它将主要是.swf。
我的url是,

I am having an application to download video on sdcard from url. I refer this link. But this link contains the url as .mp4 format. I have to download news feed videos. My format is not .mp4 format. It will be mostly .swf. The url of mine is,

http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511 / uiconf_id / 5590821 / entry_id / 0_cf39ej0c

我通过解析rss feed(news feed)得到这个url。

I was getting this url by parsing rss feed( news feed).

我的代码是:

public class MainActivity extends Activity {
    public final String TAG = "MainActivity";
    private final String PATH = "/sdcard/downloadVideo/";
    private final int TIMEOUT_CONNECTION = 5000;// 5sec
    private final int TIMEOUT_SOCKET = 300000;// 30sec
    //public final String imageURL = "http://www.cbsnews.com/video/watch/?id=50149183n&tag=api";
    public final String imageURL = "http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DownloadFromUrl(imageURL, PATH);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void DownloadFromUrl(String VideoURL, String fileName) { // this is
                                                                    // the
                                                                    // downloader
                                                                    // method
        try {
            URL url = new URL(VideoURL);
            long startTime = System.currentTimeMillis();
            Log.i(TAG, "image download beginning: " + VideoURL);

            // Open a connection to that URL.
            URLConnection ucon = url.openConnection();

            // this timeout affects how long it takes for the app to realize
            // there's a connection problem
            ucon.setReadTimeout(TIMEOUT_CONNECTION);
            ucon.setConnectTimeout(TIMEOUT_SOCKET);

            // Define InputStreams to read from the URLConnection.
            // uses 3KB download buffer
            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
            FileOutputStream outStream = new FileOutputStream(fileName);
            byte[] buff = new byte[5 * 1024];

            // Read bytes (and store them) until there is nothing more to
            // read(-1)
            int len;
            while ((len = inStream.read(buff)) != -1) {
                outStream.write(buff, 0, len);
            }

            // clean up
            outStream.flush();
            outStream.close();
            inStream.close();

            Log.i(TAG, "download completed in "
                    + ((System.currentTimeMillis() - startTime) / 1000)
                    + " sec");

        } catch (IOException e) {
            Log.d("VideoManager", "Error: " + e);
        }

    }
}

但是无法获得此视频?

But I couldn't get this video?

其实我正在试图实现视频流。但是我不知道该怎么做。

Actually I am trying to achieve video streaming. But I don't know that how to do it.

我的代码有错吗?

请提出建议,以达到上述目的。

Please give your suggestion to achieve the above.

提前谢谢!

推荐答案

尝试这样:

而不是手动定义SD卡的路径,请使用

Instead of defining the path to the SD card manually, use

String Path = Environment.getExternalStorageDirectory().getPath();

并在其后添加一个文件名,如

and add a file name behind that, like

String filename = Path + "/filename.mp4"

并将这些 filename 字符串传递给您的 FileOutputStream

and pass these filename strings to your FileOutputStreams.

编辑

而不是下载文件,直接使用VideoView直接查看,代码如下:

Instead of downloading the file, view it directly using VideoView, with code like this:

VideoView mVideoView;
MediaController mcon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoplay);
    mVideoView = (VideoView) findViewById(R.id.videoview);
    String videourl="http://cdnapi.kaltura.com/index.php/kwidget/wid/_483511/uiconf_id/5590821/entry_id/0_cf39ej0c";
    mcon.setMediaPlayer(mVideoView); // add this line
    mcon = new MediaController(this);
    mVideoView.setMediaController(mcon);
    mVideoView.requestFocus();
    mVideoView.setVideoURI(Uri.parse(videourl));
    mcon.show();
    mVideoView.start();
}

不要忘了在 androidmanifest中提及互联网权限.xml

<uses-permission android:name="android.permission.INTERNET" />

这篇关于从网址下载SDCard上的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆