4

I am making RSS reader application, and I get RSS data from URL, and that RSS data can contain link to YouTube video.

Here is an example how link to youtube vide looks like:

div class="video-shortcode"><iframe title="YouTube video player" width="600" 
height="350" src="http://www.youtube.com/embed/HXrcUyCVA6c"
frameborder="0" allowfullscreen></iframe></div>

And when I run my application, there is no video, its all black, and I can't play it.

How I can play video inside WebView?

EDIT: Here is the result:

enter image description here

Here is my code:

 // set webview properties
    WebSettings ws = desc.getSettings();
    ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    ws.getPluginState();
    ws.setPluginState(PluginState.ON);
    ws.setJavaScriptEnabled(true);
    ws.setUserAgent(0);
    ws.setJavaScriptCanOpenWindowsAutomatically(true);

    desc.setWebChromeClient(new WebChromeClient() {
    });
    desc.loadDataWithBaseURL("http://www.balkanandroid.com/", feed
    .getItem(pos).getContent(), "text/html", "UTF-8", null);

I have android:hardwareAccelerated="true" in my AndroidManifest.xml.

Zookey
  • 2,637
  • 13
  • 46
  • 80
  • Possible duplicate http://stackoverflow.com/questions/17706580/youtube-video-not-playing-in-webview – EvZ Jul 22 '13 at 08:51
  • I tried with adding setWebChromeClient and adding android:hardwareAccelerated="true", but it stil doesn't work. – Zookey Jul 22 '13 at 08:55
  • Check all the code examples there. You missing something. – EvZ Jul 22 '13 at 08:57
  • try this also http://stackoverflow.com/questions/14109083/android-webview-video-play –  Jul 22 '13 at 08:59
  • 1
    mee to was facing this issue I got my issue resolve from this http://stackoverflow.com/questions/12708890/youtube-video-not-playing-in-webview-android –  Jul 22 '13 at 09:01
  • I can't get first look of video with play button, its just black! – Zookey Jul 22 '13 at 09:12
  • Edited my question with more details. – Zookey Jul 22 '13 at 09:49
  • Follow this http://stackoverflow.com/a/39447775/2581109 its working fine. I have posted my answer here – Deepak Gupta Sep 12 '16 at 10:02
  • Try this link http://stackoverflow.com/a/39447775/2581109 its working fine.. I have posted my code here – Deepak Gupta Sep 12 '16 at 10:04

3 Answers3

9

Probably not of any use, but it might be handy for future people

But remove this line:

ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

and make sure javascript is enabled

ws.setJavaScriptEnabled(true);

These two lines caused me quite a lot of trouble when I was starting out.

David Liaw
  • 3,193
  • 2
  • 18
  • 28
5

In the android manifest, set android:hardwareAccelerated="true":

<application android:icon="@drawable/icon" android:label="@string/app_name" android:hardwareAccelerated="true">
<activity android:name=".Activity"
    android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

And in your main activity set:

WebSettings w = webView.getSettings();      
w.setPluginState(PluginState.ON);

Hope This will work for you....!!!!

2

use this code. its playing one you-tube video using web-view.its working good.

   package com.example.webbiewdemoapplication;

   import android.app.Activity;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.webkit.WebView;
   import android.widget.Button;

 public class WebviewMainActivityFirst extends Activity {

 WebView myWebview;
 Button bw1;
 @Override
 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview_main);

    myWebview = (WebView) findViewById(R.id.webview1);

    bw1 = (Button) findViewById(R.id.click_button1);
    bw1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

    Intent intent1 = new  Intent().setClass(
                    WebviewMainActivityFirst.this, WebViewActivitySecond.class);
            startActivity(intent1);
        }
    });




  }
        }

 // second activity

 package com.example.webbiewdemoapplication;

 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.os.Bundle;
 import android.webkit.WebView;


 public class WebViewActivitySecond extends Activity {

private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle saveState) {

    super.onCreate(saveState);
    setContentView(R.layout.activity_main_second);

    webView = (WebView) findViewById(R.id.webview1);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.loadUrl("https://www.youtube.com/watch?v=68AqHwgk2s8");

//  String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
//  webView.loadData(customHtml, "text/html", "UTF-8");



}

 }

// xml part

 activity_webview_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" >

<Button
    android:id="@+id/click_button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="106dp"
    android:text="Go To WebView" />

</RelativeLayout>

activity_main_second.xml

 <?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" >


 <WebView
    android:id="@+id/webview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />


 </LinearLayout>
harikrishnan
  • 1,985
  • 4
  • 32
  • 63