Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube embed videos not working in webview. What's wrong with this code?

I am new to android. I am facing two problems right now 1. I parsed xml file and showed it in webview which contains videos and text . When i click on videos it will not play . In xml videos are youtube embed.

Why is it 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 on video it starts browsing and shows only black screen,then nothing will happen.

![][1]

2. I have you-tube embedded videos url which I given in html tag and try to load it .They are also not working for me. I checked number of questions and blogs and also made no of settings then also unable to show 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 load the youtube embeded videos to webview but they are also not working.

![][2]

I am using android 2.3.3version.I also want clarification that is there any requirement of install adobe flash player on emulator, but i think no because videos are working in browser. can anybody tell i am right or wrong?? please try to give me solution because i stuck with this problem long ago..

I also tried using 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>";
like image 240
The iCoder Avatar asked Nov 14 '11 13:11

The iCoder


Video Answer


1 Answers

You need to

  • set a WebChromeClient to your WebView

        web.setWebChromeClient(new WebChromeClient());
    
  • and turn on hardwareAccelerated value

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

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

This works for me...

like image 168
ayalcinkaya Avatar answered Nov 16 '22 00:11

ayalcinkaya