【问题标题】:youtube url for android videoviewandroid videoview 的 youtube 网址
【发布时间】:2014-01-31 09:27:17
【问题描述】:

我想开发一个应用程序,它可以从VideoView 的 youtube 下载或播放视频。 我看过很多资源,例如:

  1. https://developers.google.com/youtube/2.0/reference?csw=1#Overview
  2. https://developers.google.com/youtube/v3/
  3. https://developers.google.com/youtube/android/player/
  4. https://developers.google.com/youtube/iframe_api_reference/

最后的问题是,如果我有 youtube videoid,我如何在 videoview 中流式传输它。 我知道要检索 .3gp 直播视频,但我希望在 videoview 中播放 mp4 高清视频

【问题讨论】:

  • 有没有人可以建议链接请...??

标签: android video youtube


【解决方案1】:

也许其他人可能需要这个。

private class YourAsyncTask extends AsyncTask<Void, Void, Void>
{
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(AlertDetail.this, "", "Loading Video wait...", true);
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            String url = "http://www.youtube.com/watch?v=1FJHYqE0RDg";
            videoUrl = getUrlVideoRTSP(url);
            Log.e("Video url for playing=========>>>>>", videoUrl);
        }
        catch (Exception e)
        {
            Log.e("Login Soap Calling in Exception", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        progressDialog.dismiss();
        videoView.setVideoURI(Uri.parse(videoUrl));
        MediaController mc = new MediaController(AlertDetail.this);
        videoView.setMediaController(mc);
        videoView.requestFocus();
        videoView.start();          
        mc.show();
    }

}

public static String getUrlVideoRTSP(String urlYoutube)
{
    try
    {
        String gdy = "http://gdata.youtube.com/feeds/api/videos/";
        DocumentBuilder documentBuilder =     DocumentBuilderFactory.newInstance().newDocumentBuilder();
        String id = extractYoutubeId(urlYoutube);
        URL url = new URL(gdy + id);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        Document doc = documentBuilder.parse(connection.getInputStream());
        Element el = doc.getDocumentElement();
        NodeList list = el.getElementsByTagName("media:content");///media:content
        String cursor = urlYoutube;
        for (int i = 0; i < list.getLength(); i++)
        {
            Node node = list.item(i);
            if (node != null)
            {
                NamedNodeMap nodeMap = node.getAttributes();
                HashMap<String, String> maps = new HashMap<String, String>();
                for (int j = 0; j < nodeMap.getLength(); j++)
                {
                    Attr att = (Attr) nodeMap.item(j);
                    maps.put(att.getName(), att.getValue());
                }
                if (maps.containsKey("yt:format"))
                {
                    String f = maps.get("yt:format");
                    if (maps.containsKey("url"))
                    {
                        cursor = maps.get("url");
                    }
                    if (f.equals("1"))
                        return cursor;
                }
            }
        }
        return cursor;
    }
    catch (Exception ex)
    {
        Log.e("Get Url Video RTSP Exception======>>", ex.toString());
    }
    return urlYoutube;

}

protected static String extractYoutubeId(String url) throws MalformedURLException
{
    String id = null;
    try
    {
        String query = new URL(url).getQuery();
        if (query != null)
        {
            String[] param = query.split("&");
            for (String row : param)
            {
                String[] param1 = row.split("=");
                if (param1[0].equals("v"))
                {
                    id = param1[1];
                }
            }
        }
        else
        {
            if (url.contains("embed"))
            {
                id = url.substring(url.lastIndexOf("/") + 1);
            }
        }
    }
    catch (Exception ex)
    {
        Log.e("Exception", ex.toString());
    }
    return id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2018-03-09
    • 2013-04-15
    相关资源
    最近更新 更多