YouTube video not playing in WebView - Android

I am attached to playing YouTube videos in WebView, WebView shows the first kind of video with a play button. But after you click on the start button of the play button, and after 2-3 seconds, stop the progress bar and the screen is blank with black.

Image1: first watch video with play button

Image2: after clicking on the screen, the play button becomes blank.

You are welcome! help me why the video does not start.

PICTURE: 1 This is first look of web view

IMAGES: 2 enter image description here

This is my source code for playing YouTubeVideo in web browsing. 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; } 
+42
android youtube video android-webview
Oct 03 '12 at 12:50
source share
8 answers

Add these lines before loading the HTML content into your WebView.

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

From the documentation:

To support HTML5 embedded video in your application, you need to enable hardware acceleration, and install WebChromeClient . For full-screen support, onShowCustomView (View, WebChromeClient.CustomViewCallback) and onHideCustomView () implementations are required, getVideoLoadingProgressView () is optional.

+68
Oct 05 '12 at 18:09
source share

The following code is required to display the video player launched by the web core framework. The key to the whole stream is that the VideoView is passed back to WebChromeClient, and you need to attach this view to your view hierarchy.

I compiled it by looking at the source code of the browser, available as part of AOSP.

This code refers to 4 species that may not be obvious. View Hierarchy:

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

Views are configured as part of the container activity setting.

 <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 onCreate actions:

  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 . This client should override the following 2 to 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 can also add activity lifecycle bindings, for example:

 @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(); } } 

In its activity, to make the video hidden when the action is stopped or the back button is pressed.

+17
Oct 12 '12 at 1:00
source share

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

 if (Build.VERSION.SDK_INT < 8) { webview.getSettings().setPluginsEnabled(true); } else { webview.getSettings().setPluginState(PluginState.ON); } 

Thnaks

+12
Feb 07 '13 at 4:45
source share

There is some problem with youtbe videos being broadcast on mobile devices. If you try to load the URL into a web view and run it, the video will not play. One difficult way to solve this problem is to stream video as a video. I have not tried this, but it can be done.
Another way to play YouTube videos, I will call it, the easiest way is to change the user agent in the web presentation settings from the mobile device to the desktop. The user agent indicates the type of device on which the YouTube video will run, and, accordingly, a website is sent by the server. This way you can stream and play YouTube videos. Here's how you can do it:

 public static final int USER_MOBILE = 0; public static final int USER_DESKTOP = 1; wv.getSettings().setUserAgent(USER_DESKTOP); // 1 is for the desktop 
+5
09 Oct '12 at 18:43
source share

I copied the addeks code and it worked for me .... I think you need to install the flash payer. and did you have permission on the internet?

btw my code is here ...

 package com.example.youtube; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import android.util.Log; import android.view.Menu; import android.view.Window; import android.view.WindowManager; import android.webkit.DownloadListener; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebChromeClient; import android.webkit.WebViewClient; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AbsoluteLayout; import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.os.AsyncTask; import android.util.Log; import android.widget.TextView; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; public class MainActivity extends Activity{ @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.activity_main); final ProgressBar Pbar; Pbar = (ProgressBar) findViewById(R.id.pB4); WebView wv = (WebView) findViewById(R.id.webVie1); //wv.setWebViewClient(new Callback()); WebSettings webSettings = wv.getSettings(); webSettings.setBuiltInZoomControls(true); webSettings.setJavaScriptEnabled(true); //wv.setBackgroundColor(0x919191); final String mimeType = "text/html"; final String encoding = "UTF-8"; String html = getHTML(); wv.loadDataWithBaseURL("", html, mimeType, encoding, ""); final Activity activity = this; wv.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 * 100); { if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){ Pbar.setVisibility(ProgressBar.VISIBLE); } Pbar.setProgress(progress); if(progress == 100) { Pbar.setVisibility(ProgressBar.GONE); } } } }); wv.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_LONG).show(); } }); wv.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); } }); } private String getHTML() { // TODO Auto-generated method stub String html1 = "<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 html1; } /* public void onPause() { super.onPause(); System.exit(0); }*/ } 

layout file

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/page_buttons" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> <WebView android:id="@+id/webVie1" android:layout_width="316dp" android:layout_height="392dp" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_weight="0.99" /> <ProgressBar android:id="@+id/pB4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> /> </LinearLayout> 
+2
Apr 27 '13 at 14:48
source share

Why do you want to play the pipe in web view? You can play it using this intention.

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(stringUrl))); 

Otherwise, if you want to play it in webView, follow this link

http://www.stackoverflow.com/questions/9565533/android-how-to-play-youtube-video-in-webview?rq=1

0
Oct 10
source share

my webview had javascript enabled but the removal made the video work

  webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
0
May 09 '19 at
source share

Try this one

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(stringUrl))); 
-one
Jan 24 '13 at 12:48
source share



All Articles