Friday 4 November 2011

playing youtube video

xml file=>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout android:layout_width="fill_parent"
android:gravity="center" android:id="@+id/topbar"
android:layout_height="wrap_content" android:layout_gravity="center"
android:background="@drawable/mainheading">
<Button android:layout_width="wrap_content" android:background="@drawable/back"
android:layout_height="wrap_content" android:layout_margin="5dip"
android:id="@+id/back" />
<TextView android:layout_width="fill_parent"
android:layout_marginBottom="5dip" android:layout_marginLeft="15dip"
android:id="@+id/top_text" android:textStyle="bold" android:textSize="15sp"
android:gravity="center" android:layout_gravity="center"
android:layout_height="wrap_content" android:layout_alignBottom="@+id/back" />
</RelativeLayout>
<WebView android:layout_width="wrap_content" android:id="@+id/video_play"
android:layout_below="@+id/topbar" android:layout_height="wrap_content" />

</RelativeLayout>
=======================================================
java file=>

package com.LIJSL;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class YoutubeActivity extends Activity {
/** Called when the activity is first created. */
ProgressDialog dialog;
String Link;
String video_id;

private WebView myWebView;

int device_height;
int device_width;

// Back Button

private Button back;
private TextView top_text;

Context context = null;

// screen size check
private boolean checkofmedium;
private boolean checkoflarge;
private boolean checkofNormal;

// Screen Orientation Variable

int screenOrientation;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.youtube);

checkofmedium = ScreenConfg.checkScreenSize(getWindowManager());
checkoflarge = ScreenConfg.checkScreenSize_10(getWindowManager());
checkofNormal = ScreenConfg.checkScreenSize_Normal(getWindowManager());

top_text = (TextView) findViewById(R.id.top_text);
top_text.setText("VIDEO");

screenOrientation = getScreenOrientation();

back = (Button) findViewById(R.id.back);

back.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
android.os.Process.killProcess(android.os.Process.myPid());

// onBackPressed();

myWebView.clearAnimation();
myWebView.clearHistory();
myWebView.clearFormData();
myWebView.clearCache(true);

finish();

}
});

Bundle extras = getIntent().getExtras();
if (extras != null) {
Link = extras.getString("Link");
}

// if (checkofmedium) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// } else if (checkoflarge) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// } else
if (checkofNormal) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

WindowManager wm = getWindowManager();
Display display = wm.getDefaultDisplay();

device_width = display.getWidth();
device_height = display.getHeight();

String j = Link.substring(Link.indexOf("v=") + 2);
video_id = j.substring(0, 11);

dialog = ProgressDialog.show(this, "", "Loading Data ...Please Wait",
true);

if (Internet_Check.checkInternetConnection(getApplicationContext())) {
video_display.start();
} else {
Alert_dialog();
dialog.dismiss();
}

}

@Override
protected void onStop() {
super.onStop();
if (dialog != null) {
dialog.dismiss();
}
}

Thread video_display = new Thread() {
public void run() {
myWebView = (WebView) findViewById(R.id.video_play);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(
false);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setSupportMultipleWindows(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.setVerticalScrollBarEnabled(false);
myWebView.setHorizontalScrollBarEnabled(false);

if (screenOrientation == 1) {
if (checkofmedium) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 380
+ "\" height=\""
+ 600
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");
} else if (checkoflarge) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 780
+ "\" height=\""
+ 1100
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");
} else if (checkofNormal) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 300
+ "\" height=\""
+ 420
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");

}
} else if (screenOrientation == 2) {
if (checkofmedium) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 670
+ "\" height=\""
+ 330
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");
} else if (checkoflarge) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 1260
+ "\" height=\""
+ 650
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");
} else if (checkofNormal) {
String playVideo = "<iframe class=\"youtube-player\" type=\"text/html\" width=\""
+ 300
+ "\" height=\""
+ 420
+ "\" src=\"http://www.youtube.com/embed/"
+ video_id + "\" frameborder=\"0\">";

myWebView.loadData(playVideo, "text/html", "utf-8");
}
}
handler.sendEmptyMessage(0);
}
};

final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};

private void Alert_dialog() {
// Create the dialog box
AlertDialog.Builder alertbox = new AlertDialog.Builder(
YoutubeActivity.this);
alertbox.setTitle("Connection Error");
// Set the message to display
alertbox.setMessage("Please Check is your internet connection");
// Set a positive/yes button and create a listener
alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// Click listener
public void onClick(DialogInterface arg0, int arg1) {
YoutubeActivity.this.finish();
}
});
// display box
alertbox.show();

}

public int getScreenOrientation() {
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if (getOrient.getWidth() == getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT)
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());

// onBackPressed();

Log.v("log_tag", "Android Back Event ");

myWebView.clearAnimation();
myWebView.clearHistory();
myWebView.clearFormData();
myWebView.clearCache(true);

this.finish();
}

}

No comments:

Post a Comment