How to play Youtube url in WebView or VideoView

I just want to play YouTube url in WebView or VideoView. Suppose this . I followed the number of tutorials. But I cannot find a way.

WebViewActivity

In onCreate ()

webView = (WebView) findViewById(R.id.webview);

webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setPluginState(PluginState.ON);

// webView.getSettings().setPluginsEnabled(true);

final Activity activity = this;

webView.setWebChromeClient(new WebChromeClient()
{
    public void onProgressChanged(WebView view , int progress)
    {
        // Activities and WebViews measure progress with different
        // scales.
        // The progress meter will automatically disappear when we reach
        // 100%
        activity.setProgress(progress * 1000);
    }
});

webView.setWebViewClient(new MyOwnWebViewClient());


webView.loadData(playVideo, "text/html", "utf-8");

//webView.loadUrl("http://www.androidbegin.com/tutorial/AndroidCommercial.3gp");
//webView.loadUrl("file:///android_asset/Videos/" + extra + ".htm");
webView.setWebViewClient(new MyOwnWebViewClient());

      

And MyOwnWebViewClient

public class MyOwnWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view , String url)
        {
            view.loadUrl(url);

            return true;
        }
    }

      

VideoActivity

In onCreate ()

new VideoViewTask(VideoActivity.this, videoView).execute();

      

Where VideoViewTask.java

public class VideoViewTask extends AsyncTask<String, Void, String>
{
    ProgressDialog progressDialog;

    Context contexto;

    VideoView videoViewo;

    public VideoViewTask(Context context,VideoView videoView)
    {
        contexto = context;

        videoViewo = videoView;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(contexto, "", "Loading Video wait...", true);
    }
    @Override
    protected String doInBackground(String... arg0)
    {
        String videoUrl = "";
        try
        {
            String url =  "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";//"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 videoUrl;
    }


    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);

        progressDialog.dismiss();

        videoViewo.setVideoURI(Uri.parse(result));
        MediaController mc = new MediaController(contexto);
        videoViewo.setMediaController(mc);
        videoViewo.requestFocus();
        videoViewo.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;
    }
}

      

I just want to reproduce the url url in my application. I also tried this by callingwatchYoutubeVideo("oKiYuIsPxYk");

Where

public void watchYoutubeVideo(String id)
    {
        try
        {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));

            startActivity(intent);

        }
        catch (ActivityNotFoundException ex)
        {
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://www.youtube.com/watch?v=" + id));
            startActivity(intent);
        }
    }

      

+3


source to share


1 answer


please follow the example below. I've tried with android developers video.its working fine.playing with app only.

 WebView wv=(WebView)findViewById(R.id.webview);
     wv.getSettings().setJavaScriptEnabled(true);
    //wv.getSettings().setPluginsEnabled(true);
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    String html = getHTML();
    wv.setWebChromeClient(new WebChromeClient() {
    });
    wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

      

add above code to oncreate



public String getHTML() {
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
        + "s-4J7cijPAo"
        + "?fs=0\" frameborder=\"0\">\n"
        + "</iframe>\n";
return html;

      

insert your youtube video id as shown above.

0


source







All Articles