Duplicate seems to play YouTube videos in WebView and YouTube doesn't play in WebView - Android
To make it work through WebView, I used the following two steps (version 4.2.2 / Nexus 4):
In shouldOverrideUrlLoading I added webview.setWebChromeClient(new WebChromeClient());
In AndroidManifest.xml for the activity tag, I added android:hardwareAccelerated="true"
AndroidManifest.xml must also contain permission to use the Internet.
While the exact steps of playing videos through WebView may be specific to the device and version of Android, there are also other alternatives to WebView, such as VideoView or using the Youtube Android Player API.
EDIT1
Regarding the pause and full screen, this is actually the second link that I mentioned at the beginning. To keep things simple, I am posting code that worked on my Nexus.
In activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <FrameLayout android:id="@+id/fullscreen_custom_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF000000"/> <LinearLayout android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </RelativeLayout>
In the MainActivity class:
public class MainActivity extends Activity { private WebView mWebView; private LinearLayout mContentView; private FrameLayout mCustomViewContainer; private WebChromeClient.CustomViewCallback mCustomViewCallback; FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentView = (LinearLayout) findViewById(R.id.linearlayout); mWebView = (WebView) findViewById(R.id.webView); mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content); WebSettings webSettings = mWebView.getSettings(); webSettings.setPluginState(WebSettings.PluginState.ON); webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); mWebView.loadUrl("http://www.google.com"); mWebView.setWebViewClient(new HelloWebViewClient()); } @Override public boolean onCreateOptionsMenu(Menu menu) {
EDIT2 - Add Back Button Support
public class MainActivity extends Activity { private WebView mWebView; private LinearLayout mContentView; private FrameLayout mCustomViewContainer; private View mCustomView; private WebChromeClient.CustomViewCallback mCustomViewCallback; FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER); private WebChromeClient mWebChromeClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentView = (LinearLayout) findViewById(R.id.linearlayout); mWebView = (WebView) findViewById(R.id.webView); mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content); mWebChromeClient = new WebChromeClient() { @Override public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
source share