youtube url for android videoview

TechArcSri

I want to develop an app which downloads or plays a video from youtube in VideoView. I've seen many resources like:

  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/

Finally the question is if i have the youtube videoid how can i stream it in videoview. I know to retrieve .3gp live streaming video but i want mp4 hd video to play in videoview

TechArcSri

May someone else may need this.

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to play VIMEO video url in android videoview

From Dev

Play video from url in VideoView [Android]

From Dev

Play a youtube url in android

From Dev

VideoView Android

From Dev

Android VideoView playing video from url file extension required?

From Dev

Getting Access on the Android VideoView received packets when Streaming a URL

From Dev

How to create videoview like youtube "small screen and full screen play in android "

From Dev

mvvmcross videoview URL binding

From Dev

Center Crop an Android VideoView

From Dev

Android load video in VideoView

From Dev

Android VideoView SplashScreen

From Dev

Android connecting MediaPlayer to VideoView

From Dev

android fragment in videoview in tab

From Dev

VideoView in CustomDialog Android

From Dev

Navigation Drawer and VideoView in android

From Dev

Play video in VideoView android

From Dev

Playing video in videoview android

From Dev

Corner for VideoView - Android

From Dev

VideoView Loader Android

From Dev

Android VideoView buffering

From Dev

Unable to stream YouTube Url in VLC android

From Dev

android youtube download url 403 forbidden

From Dev

Get URL of video running in Youtube android app

From Dev

android youtube download url 403 forbidden

From Dev

How to load the URL of youtube in webview android?

From Dev

Can not play a mp4 video in android VIdeoview from a remote url

From Dev

Can not play a mp4 video in android VIdeoview from a remote url

From Dev

How can I know the real time the video starts in android VideoView with the video view from URL?

From Java

Android videoview - immersive - overlapping controllers