YouTube Video no se reproduce en WebView

YouTube Video no se reproduce en WebView.

Es mi primera aplicación, quiero hacer una vista web. Cuando abro YouTube el video no se reproduce, se está cargando pero no se está reproduciendo. Está cargando todo el tiempo. Muchas gracias por ayudar.

Java:

public class MainActivity extends Activity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://www.google.com"); mWebView.setWebViewClient(new HelloWebViewClient()); } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webview, String url) { webview.loadUrl(url); return true; }} @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } } 

Xml:

   

Parece que el video duplicado de youtube en WebView y YouTube Video no se reproduce en WebView – Android

Para que funcione a través de WebView, utilicé los siguientes dos pasos (versión 4.2.2 / Nexus 4):

  1. En shouldOverrideUrlLoading agregué webview.setWebChromeClient(new WebChromeClient());

  2. En AndroidManifest.xml para la etiqueta de activity agregué android:hardwareAccelerated="true"

AndroidManifest.xml también debe contener un permiso de Internet.

Si bien los pasos exactos con la reproducción de video a través de WebView pueden ser específicos para el dispositivo y la versión de Android, también existen otras alternativas a WebView, como VideoView o el uso de la API del reproductor de Android de Youtube.

EDIT1

En cuanto a la pausa y la pantalla completa, este es en realidad un segundo enlace que mencioné al principio. Para hacerlo más fácil, estoy publicando código que funcionaba en mi Nexus.

  1. En activity_main.xml

           
  2. En la clase MainActivity:

     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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webview, String url) { webview.setWebChromeClient(new WebChromeClient() { private View mCustomView; @Override public 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(); } }); webview.loadUrl(url); return true; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } } 

EDIT2Agregar soporte de botón hacia atrás

 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) { // 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(); } @Override public 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); } }; 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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webview, String url) { webview.setWebChromeClient(mWebChromeClient); webview.loadUrl(url); return true; } } @Override protected void onStop() { super.onStop(); if (mCustomView != null) { if (mCustomViewCallback != null) mCustomViewCallback.onCustomViewHidden(); mCustomView = null; } } @Override public void onBackPressed() { super.onBackPressed(); if (mCustomView != null) { mWebChromeClient.onHideCustomView(); } else { finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } } 

Agregar el cliente webchrome resuelve el problema,

 web = (WebView) vi.findViewById(R.id.offer_webView1); web.loadUrl(getResources().getString(R.string.videolink)); web.getSettings().setJavaScriptEnabled(true); // web.getSettings().setDomStorageEnabled(true); web.getSettings().setAllowContentAccess(true); WebSettings webSettings = web.getSettings(); webSettings.setPluginState(WebSettings.PluginState.ON); webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); web.canGoBack(); web.setWebChromeClient(new WebChromeClient() {});