In this blog i am going to explain how to play embeded Youtube video on webview. am loading the video file in an iframe. While Loading the html page my webview is loading like,
Here is my HTML Page:
<html>
<head> </head>
<body>
<iframe width="560" height="315" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/YfQW6cpRHlo" frameborder="0" allowfullscreen>
</iframe>
</body>
</html>
Here the embeded video link is "http://www.youtube.com/embed/YfQW6cpRHlo". In this link "YfQW6cpRHlo" is the Id of Video file. If you want to play another video you just replace the video id on this link.
Android Code
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"/>
</LinearLayout>
MainActivity.Java
Here "http://example.com/shidhin/test.html" is my webpage Link.You Can replace it by your Own Link.
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private WebView webView;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
webView = (WebView) findViewById(R.id.webView1);
progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...",true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.loadUrl("http://example.com/shidhin/test.html");
webView.setWebChromeClient(new WebChromeClient() {
});
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
//Toast.makeText(context, "Page Load Finished", Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml
Here I have added android:hardwareAccelerated="true" On Application tag.
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Also Dont Forgot to Add Permissions on the Manifest file. We need Internet Permission to get the Internet Connection.
<uses-permission android:name="android.permission.INTERNET" />