Here's the code to play YouTube videos in WebView using video url.
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=".VideoActivity" >
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_margin="5dp" />
<WebView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
Youtube Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtube"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.youtube.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
YoutubeActivity.java
package com.example.youtube;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
public static final int USER_MOBILE = 0;
public static final int USER_DESKTOP = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView video = (WebView) findViewById(R.id.videoView);
Button btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setText("Play Video");
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
video.getSettings().setJavaScriptEnabled(true);
video.getSettings().setPluginState(WebSettings.PluginState.ON);
//video.getSettings().getDefaultUserAgent(USER_MOBILE);
video.setWebChromeClient(new WebChromeClient() {
});
//youtube video url
////http://www.youtube.com/watch?v=WM5HccvYYQg
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML("PT8zRmrUTrA");
video.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
});
}
public String getHTML(String videoId) {
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/" + videoId
+ "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
+ "controls onclick=\"this.play()\">\n" + "</iframe>\n";
return html;
}
}
No comments:
Post a Comment