Thursday, May 24, 2012

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);

}

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;
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);

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() {
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());
}
}

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;
}

// 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);
}

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

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

7 comments:

Unknown said...

if i replaced the youtube video url with a shared url of a avi video on a machine...
will it work?

Unknown said...

i need live streaming to connect android with pc(jsp/servlet) connection.

Unknown said...

android app development

Akash said...

Cortana

Unknown said...

@youtube link plat not diretly they need to register your app in youtube develpper

Anonymous said...

Informative,Learn to create great Video Tutorials,Streaming, Corporate Videos & Animations.Learn to shoot with the right ISO, Exposure & Aperture and then learn to edit them. http://bit.ly/1NcSH0V

Unknown said...

I am getting prepared failed error can you please help ??