You are on page 1of 14

pdfcrowd.com open in browser PRO version Are you a developer?

Try out the HTML to PDF API


2

More

Next Blog Create Blog

Sign In
Android World
T h u r s d a y , 2 4 M a y 2 0 1 2
Android Video Streaming Example
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class HelloAndroidActivity extends Activity implements OnClickListener {
private static String TAG = "androidEx2";
private Button buttonVideoSample;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.main);
buttonVideoSample = (Button) findViewById(R.id.buttonVideoSample);
buttonVideoSample.setOnClickListener(this);

}
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
public void onClick(View v) {

if (v.getId() == R.id.buttonVideoSample)

{
// http://podcast.20min-tv.ch/podcast/20min/199733.mp4
// HERE SET YOUR VIDEO URI http://www.youtube.com/v/ZirgAYBcOgo&hl=en&fs=1
String video_uri = "http://www.youtube.com/watch?v=cxLG2wtE7TM";
Intent intent = new Intent(this, VideoSample.class);
intent.putExtra("video_path", video_uri);
startActivity(intent);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/buttonVideoSample"
android:text="Click to Stream video">

</Button>

</LinearLayout>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class VideoSample extends Activity implements OnSeekBarChangeListener, Callback,
OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener,
OnClickListener, OnSeekCompleteListener, AnimationListener {

private TextView textViewPlayed;
private TextView textViewLength;
private SeekBar seekBarProgress;
private SurfaceView surfaceViewFrame;
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
private ImageView imageViewPauseIndicator;
private MediaPlayer player;
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private Timer updateTimer;
private Bundle extras;
private Animation hideMediaController;
private LinearLayout linearLayoutMediaController;
private static final String TAG = "androidEx2 = VideoSample";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videosample);
extras = getIntent().getExtras();
linearLayoutMediaController = (LinearLayout)
findViewById(R.id.linearLayoutMediaController);
linearLayoutMediaController.setVisibility(View.GONE);
hideMediaController = AnimationUtils.loadAnimation(this, R.anim.disapearing);
hideMediaController.setAnimationListener(this);
imageViewPauseIndicator = (ImageView) findViewById(R.id.imageViewPauseIndicator);
imageViewPauseIndicator.setVisibility(View.GONE);

if (player != null) {
if (!player.isPlaying()) {
imageViewPauseIndicator.setVisibility(View.VISIBLE);
}
}
textViewPlayed = (TextView) findViewById(R.id.textViewPlayed);
textViewLength = (TextView) findViewById(R.id.textViewLength);
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
surfaceViewFrame.setOnClickListener(this);
surfaceViewFrame.setClickable(false);
seekBarProgress = (SeekBar) findViewById(R.id.seekBarProgress);
seekBarProgress.setOnSeekBarChangeListener(this);
seekBarProgress.setProgress(0);
progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
holder = surfaceViewFrame.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
player = new MediaPlayer();
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnBufferingUpdateListener(this);
player.setOnSeekCompleteListener(this);
player.setScreenOnWhilePlaying(true);
player.setDisplay(holder);
}
private void playVideo() {
if (extras.getString("video_path").equals("VIDEO_URI")) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v)
method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(extras.getString("video_path"));
player.prepare();
} catch (IllegalArgumentException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalArgumentException ===========");
e.printStackTrace();
} catch (IllegalStateException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalStateException ===========");
e.printStackTrace();
} catch (IOException e) {
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, "========== IOException ===========");
e.printStackTrace();
}
}
}).start();
}
}
private void showToast(final String string) {
runOnUiThread(new Runnable() {
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
public void run() {
Toast.makeText(VideoSample.this, string, Toast.LENGTH_LONG).show();
finish();
}
});
}
private void hideMediaController() {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
public void run() {
linearLayoutMediaController.startAnimation(hideMediaController);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.i(TAG, "========== onProgressChanged : " + progress + " from user: " + fromUser);
if (!fromUser) {
textViewPlayed.setText(Utils.durationInSecondsToString(progress));
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
if (player.isPlaying()) {
progressBarWait.setVisibility(View.VISIBLE);
player.seekTo(seekBar.getProgress() * 1000);
Log.i(TAG, "========== SeekTo : " + seekBar.getProgress());
}
}
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
// player.stop();
}
public void onPrepared(MediaPlayer mp) {
Log.i(TAG, "========== onPrepared ===========");
int duration = mp.getDuration() / 1000; // duration in seconds
seekBarProgress.setMax(duration);
textViewLength.setText(Utils.durationInSecondsToString(duration));
progressBarWait.setVisibility(View.GONE);
// Get the dimensions of the video
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
Log.i(TAG, "VIDEO SIZES: W: " + videoWidth + " H: " + videoHeight + " PROP: " +
videoProportion);
// Get the width of the screen
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
Log.i(TAG, "VIDEO SIZES: W: " + screenWidth + " H: " + screenHeight + " PROP: " +
screenProportion);
// Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
}
// Commit the layout parameters
surfaceViewFrame.setLayoutParams(lp);
// Start video
if (!player.isPlaying()) {
player.start();
updateMediaProgress();
linearLayoutMediaController.setVisibility(View.VISIBLE);
hideMediaController();
}
surfaceViewFrame.setClickable(true);
}
public void onCompletion(MediaPlayer mp) {
mp.stop();
if (updateTimer != null) {
updateTimer.cancel();
}
finish();
}
/**
* Change progress of mediaController
* */
private void updateMediaProgress() {
updateTimer = new Timer("progress Updater");
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
seekBarProgress.setProgress(player.getCurrentPosition() / 1000);
}
});
}
}, 0, 1000);
}
public void onBufferingUpdate(MediaPlayer mp, int percent) {
int progress = (int) ((float) mp.getDuration() * ((float) percent / (float) 100));
seekBarProgress.setSecondaryProgress(progress / 1000);
}
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
public void onClick(View v) {
if (v.getId() == R.id.surfaceViewFrame) {
if (linearLayoutMediaController.getVisibility() == View.GONE) {
linearLayoutMediaController.setVisibility(View.VISIBLE);
hideMediaController();
} else if (player != null) {
if (player.isPlaying()) {
player.pause();
imageViewPauseIndicator.setVisibility(View.VISIBLE);
} else {
player.start();
imageViewPauseIndicator.setVisibility(View.GONE);
}
}
}
}
public void onSeekComplete(MediaPlayer mp) {
progressBarWait.setVisibility(View.GONE);
}
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
linearLayoutMediaController.setVisibility(View.GONE);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayoutRoot"
>

<SurfaceView
android:id="@+id/surfaceViewFrame"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content">

</SurfaceView>

<LinearLayout

android:layout_width="match_parent"
android:id="@+id/linearLayoutMediaController"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:background="@color/media_controller_bg_color"
>

<TextView
android:layout_width="wrap_content"
android:id="@+id/textViewPlayed"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColor="@color/media_controller_text_color"
android:textStyle="bold"
android:text="0:00:00"
android:padding="0dp"
android:textSize="13sp"
android:gravity="center"
android:layout_height="wrap_content">

</TextView>

<SeekBar
android:id="@+id/seekBarProgress"
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
android:layout_weight="1"
style="@style/MyCustomProgressStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="50">

</SeekBar>

<TextView
android:layout_width="wrap_content"
android:id="@+id/textViewLength"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColor="@color/media_controller_text_color"
android:textStyle="bold"
android:text="0:00:00"
android:textSize="13sp"
android:padding="0dp"
android:gravity="center"
android:layout_height="wrap_content">

</TextView>

</LinearLayout>

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/progressBarWait">

</ProgressBar>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewPauseIndicator"
android:layout_gravity="center"
android:src="@drawable/pause_button">

</ImageView>

</FrameLayout>
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Posted by Abhinaw Tripathi at 22:52
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest
+2 Recommend this on Google
2 comments:
1.
</LinearLayout>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Utils {
public static String durationInSecondsToString(int sec){
int hours = sec / 3600;
int minutes = (sec / 60) - (hours * 60);
int seconds = sec - (hours * 3600) - (minutes * 60) ;
String formatted = String.format("%d:%02d:%02d", hours, minutes, seconds);
return formatted;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<uses-permission android:name="android.permission.INTERNET" />
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Karim El Tel20 April 2013 07:38
if i replaced the youtube video url with a shared url of a avi video on a machine...
will it work?
ReplyDelete
2.
Tema staff23 February 2014 05:52
i need live streaming to connect android with pc(jsp/servlet) connection.
ReplyDelete
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Comment as: Select profile...
Publish

Preview
Load more...
Newer Post Older Post Home
Subscribe to: Post Comments (Atom)
Blog Archive
2014 (1)
April (1)
2012 (3)
May (3)
Lets do sign(Signature) on android
phone
Android Video Streaming Example
Map Dispaly on Android Phone
About Me
Abhinaw Tripathi
Follow 214
I am simple
1. "I can be smart when its important but most
men don't like it"
2."Its better to be looked over than be
overlooked"
that's it.
View my complete profile
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Simple template. Powered by Blogger.

You might also like