Youtube embeds video not working in webview. What is wrong with this code?

I am new to android. Now I am facing two problems 1 . I parsed the xml file and showed it in a webview that contains video and text. When I click on the video, it will not play. In the xml video you embed youtube.

Why is this so?

1. First problem: code and image

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_view); Intent intent=getIntent(); String urlsting=intent.getStringExtra("str"); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setPluginState(PluginState.ON); mWebView.getSettings().setPluginsEnabled(true); mWebView.setInitialScale(100); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.loadDataWithBaseURL(null, urlsting,"text/html", "utf-8", null); } 

When I clcik to the video, it starts watching and shows only a black screen, then nothing will happen.

! [] [one]

2. I have a url-embedded video, which I gave in the html tag and am trying to download it. They also do not work for me. I checked the number of questions and blogs, and also did not make any settings, and then could not show the video. Help will be appreciated. Thanks in advance.

 String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/lBMMTeuJ_UQ?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/BZMkY3y7nM0?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td></table>"; mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setPluginState(PluginState.ON); mWebView.getSettings().setPluginsEnabled(true); //mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null); mWebView.loadData(video,"text/html","UTF-8"); 

In this case, I upload youtube embedded videos to webview, but they also don't work.

! [] [2]

I use android 2.3.3version.I also want to clarify that there are any requirements to install Adobe Flash Player on the emulator, but I think not, because the videos work in the browser. can anyone say that i am right or wrong? try to give me a solution because I have long been stuck in this problem ..

I also tried using the object tag as follows:

  String obj="<object width=\"300\" height=\"260\"><param name=\"movie\" value=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"?version=3&amp;hl=pt_BR&amp;rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\" ?version=3&amp;hl=pt_BR&amp;rel=0\" type=\"application/x-shockwave-flash\" width=\"480\" height=\"330\" allowscriptaccess=\"always\" allowfullscreen=\"true\" /></object>"; 
+7
android youtube video android-webview embed
Nov 14 '11 at 13:30
source share
5 answers

You need

  • set webchromeclient for your webview

      web.setWebChromeClient(new WebChromeClient()); 
  • and enable hardware accelerated value

     <application android:hardwareAccelerated="true"... 

check out http://developer.android.com/reference/android/webkit/WebView.html and read the HTML5 video support part.

This works for me ...

+8
Dec 25
source share

I think you are using the iframe tag in your html code. Modify it using the object tag. I mean, you should use an object instead of an iframe. An example is here:

 <object width="480" height="330"><param name="movie" value="http://www.youtube.com/v/"here is your video id"?version=3&amp;hl=pt_BR&amp;rel=0"> </param><param name="allowFullScreen" value="true"></param> <param name="allowscriptaccess" value="always"></param> <embed src="http://www.youtube.com/v/"here is your video id"?version=3&amp;hl=pt_BR&amp;rel=0" type="application/x-shockwave-flash" width="480" height="330" allowscriptaccess="always" allowfullscreen="true" /></object> 

Use the following code:

 webview.getSettings().setPluginsEnabled(true); webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setAllowFileAccess(true); webview.loadUrl(url); 

Try this and push it as usual if your problem is resolved :)

+2
Nov 14 '11 at 13:38
source share

Have you tested your application on the device itself? Because videos that are embedded, such as iFrame, take longer to load. In my case, the same thing happened, then I changed the settings for web browsing,

 getSettings().setPluginsEnabled(true); 

but after that it didn’t work on the emulator, and it booted up in the device, so I thought it didn’t work, but only today I tried the same thing and it worked perfectly with my surprise. So you just need to add this line of code.

Secondly, only those videos that are embedded in the iframe tag are supported by including plugins in true.

+2
03 Dec '11 at 13:15
source share

It does:

 myWebView.getSettings().setPluginState(PluginState.ON); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.getSettings().setAllowFileAccess(true); myWebView.setWebChromeClient(new WebChromeClient()); 

And turn on hardware acceleration!

+1
Sep 25 '13 at 19:34
source share

Follow this source, youtube video should play

  String youtubeId = "iiLepwjBhZE"; int width = rl.getWidth(); int height = rl.getHeight(); WebChromeClient mWebChromeClient = new WebChromeClient(){ public void onProgressChanged(WebView view, int newProgress) { /* view.loadUrl("javascript:(function() { " + "document.querySelector('iframe').setAttribute('style', 'width: 1080px; height: 1920px'); " + "})()"); */ } }; String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"" + width + "\" height=\"" + height + "\"frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/" + youtubeId + "?enablejsapi=1&origin=example.com\"></iframe> </td> </tr><tr><td></table>"; 

//

  webView.getSettings().setPluginState(PluginState.ON); webView.setWebChromeClient(mWebChromeClient); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAppCacheEnabled(true); webView.setInitialScale(1); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); //mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null); webView.loadData(video,"text/html","UTF-8"); 
+1
Feb 28 '14 at 2:15
source share



All Articles