Seeking forward in YouTube HTML5 video over Android WebView Seeking forward in YouTube HTML5 video over Android WebView android android

Seeking forward in YouTube HTML5 video over Android WebView


For future viewers

YouTube now has a player API:https://developers.google.com/youtube/android/player/

Where you can easily place a view in the xml and handle it in activity, here are the examples:https://github.com/youtube/yt-android-player


If anybody wants to use WebView yet to play youtube videos, two considerations:

  1. Depending on Android version, it can't be possible. Android version 4.x through WebView can't do that (or i didn't find the way :) As I postedhere
  2. If you are in Android 5 or upper, you have to set the WebChromeClient to the WebView component: setWebChromeClient(new WebChromeClient());

Hope it helps!


Try This One:

    wv.getSettings().setPluginsEnabled(true);    DisplayMetrics metrics = new DisplayMetrics();    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);    int w1 = (int) (metrics.widthPixels / metrics.density), h1 = w1 * 3 / 5;                    String item="http://www.youtube.com/embed/3FFyT039tJ0";    wv.getSettings().setJavaScriptEnabled(true);    wv.setWebChromeClient(chromeClient);    wv.getSettings().setPluginsEnabled(true);    try {        wv.loadData(                "<html><body><iframe class=\"youtube-player\" type=\"text/html5\" width=\""                        + (w1 - 20)                        + "\" height=\""                        + h1                        + "\" src=\""                        + item                        + "\" frameborder=\"0\"\"allowfullscreen\"></iframe></body></html>",                "text/html5", "utf-8");    } catch (Exception e) {        // TODO: handle exception        e.printStackTrace();    }}

And write a webchrome client like this:

private WebChromeClient chromeClient = new WebChromeClient() {    @Override    public void onShowCustomView(View view, CustomViewCallback callback) {        super.onShowCustomView(view, callback);        if (view instanceof FrameLayout) {            FrameLayout frame = (FrameLayout) view;            if (frame.getFocusedChild() instanceof VideoView) {                VideoView video = (VideoView) frame.getFocusedChild();                frame.removeView(video);                video.start();            }        }    }};