Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Video not playing in WebView - Android

I am tying to play YouTube video in WebView, WebView showing first look of video with play button, But after click on play button start progress bar and after 2-3 seconds stop progress bar and screen blank with black color.

Image1: Video first look with play button

Image2: After click on play button screen goes blank.

Please! help me why video not starting.

IMAGE:1 This is first look of web view

IMAGE:2 enter image description here

This is my source code to play YouTubeVideo in webview.. Please help me ...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView wv = (WebView) findViewById(R.id.webView1);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginsEnabled(true);
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    String html = getHTML();
    wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}

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/"
            + "J2fB5XWj6IE"
            + "?fs=0\" frameborder=\"0\">\n"
            + "</iframe>\n";
    return html;
}
like image 426
Ranjit Chandel Avatar asked Oct 03 '12 12:10

Ranjit Chandel


3 Answers

Add these lines before loading HTML content to your WebView.

wv.setWebChromeClient(new WebChromeClient() {
});

From the documentation:

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient. For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.

like image 110
Łukasz Sromek Avatar answered Nov 13 '22 11:11

Łukasz Sromek


The following code is needed to show the video player that is started by the web core framework. The key to the entire flow is that the VideoView is passed back to the WebChromeClient and you need to attach that view to your view hierarchy.

I have assembled it through reviewing the Browser source code available as part of the AOSP.

This code references 4 Views which may not be obvious. The view hierarchy is:

  • FrameLayout mContentView (root)
  • WebView mWebView (child of mContentView)
  • FrameLAyout mCustomViewContainer (child of mContentView)
  • View mCustomView (child of mCustomViewContainer)

The views are configured as part of setting up the container activity.

<FrameLayout
    android:id="@+id/fullscreen_custom_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000"/>

    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <WebView
            android:id="@+id/webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>
</FrameLayout>

In your Activities onCreate:

    mContentView = (FrameLayout) findViewById(R.id.main_content);
    mWebView = (WebView) findViewById(R.id.webView);
    mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);

Register a WebChromeClient with mWebView. That client should override the following 2 - 4 methods:

void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
    // if a view already exists then immediately terminate the new one
    if (mCustomView != null)
    {
        callback.onCustomViewHidden();
        return;
    }

    // Add the custom view to its container.
    mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
    mCustomView = view;
    mCustomViewCallback = callback;

    // hide main browser view
    mContentView.setVisibility(View.GONE);

    // Finally show the custom view container.
    mCustomViewContainer.setVisibility(View.VISIBLE);
    mCustomViewContainer.bringToFront();
}

void onHideCustomView()
{
    if (mCustomView == null)
        return;

    // Hide the custom view.
    mCustomView.setVisibility(View.GONE);
    // Remove the custom view from its container.
    mCustomViewContainer.removeView(mCustomView);
    mCustomView = null;
    mCustomViewContainer.setVisibility(View.GONE);
    mCustomViewCallback.onCustomViewHidden();

    // Show the content view.
    mContentView.setVisibility(View.VISIBLE);
}

public Bitmap getDefaultVideoPoster()
{
    if (mDefaultVideoPoster == null)
    {
        mDefaultVideoPoster = BitmapFactory.decodeResource(getResources(), R.drawable.default_video_poster);
    }
    return mDefaultVideoPoster;
}

public View getVideoLoadingProgressView()
{
    if (mVideoProgressView == null)
    {
        LayoutInflater inflater = LayoutInflater.from(this);
        mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);
    }
    return mVideoProgressView;
}

You may also want to add activity life-cycle bindings like:

@Override
protected void onStop()
{
    super.onStop();
    if (mCustomView != null)
    {
        if (mCustomViewCallback != null)
            mCustomViewCallback.onCustomViewHidden();
        mCustomView = null;
    }
}

@Override
public void onBackPressed()
{
    if (mCustomView != null)
    {
        onHideCustomView();
    } else
    {
        finish();
    }
}

To your activity to make the video hide when the activity is stopped or the back button is pressed.

like image 18
Nick Campion Avatar answered Nov 13 '22 11:11

Nick Campion


Add webview.setWebChromeClient(new WebChromeClient()); and to enable plugins for your video add:

if (Build.VERSION.SDK_INT < 8) {
            webview.getSettings().setPluginsEnabled(true);
        } else {
            webview.getSettings().setPluginState(PluginState.ON);
        }
like image 12
2 revs, 2 users 84% Avatar answered Nov 13 '22 13:11

2 revs, 2 users 84%