Play video from Youtube.com : Video « Media « Android






Play video from Youtube.com

  

package app.test;

import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.MediaController;

public class Test extends Activity implements
    OnCompletionListener, OnErrorListener, OnInfoListener,
    OnBufferingUpdateListener, OnPreparedListener, OnSeekCompleteListener,
    OnVideoSizeChangedListener, SurfaceHolder.Callback,
    MediaController.MediaPlayerControl {

  MediaController controller;
  Display currentDisplay;
  SurfaceView surfaceView;
  SurfaceHolder surfaceHolder;
  MediaPlayer mediaPlayer;

  View mainView;
  TextView statusView;

  int videoWidth = 0,videoHeight = 0;

  boolean readyToPlay = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mainView = this.findViewById(R.id.MainView);

    statusView = (TextView) this.findViewById(R.id.StatusTextView);

    surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView);
    surfaceHolder = surfaceView.getHolder();

    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mediaPlayer = new MediaPlayer();

    statusView.setText("MediaPlayer Created");

    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnVideoSizeChangedListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);

    String filePath = "rtsp://youtube.com/video.3gp";
    try {
      mediaPlayer.setDataSource(filePath);
    } catch (Exception e) {
      finish();
    }

    statusView.setText("MediaPlayer DataSource Set");
    currentDisplay = getWindowManager().getDefaultDisplay();
    controller = new MediaController(this);
  }

  public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer.setDisplay(holder);
    statusView.setText("MediaPlayer Display Surface Set");

    try {
      mediaPlayer.prepareAsync();
    } catch (Exception e) {
      finish();
    }

    statusView.setText("MediaPlayer Preparing");
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
  }
  public void surfaceDestroyed(SurfaceHolder holder) {
  }
  public void onCompletion(MediaPlayer mp) {
    statusView.setText("MediaPlayer Playback Completed");
  }

  public boolean onError(MediaPlayer mp, int whatError, int extra) {
    statusView.setText("MediaPlayer Error");
    if (whatError == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
      Log.v("", "Media Error, Server Died " + extra);
    } else if (whatError == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
      Log.v("", "Media Error, Error Unknown " + extra);
    }
    return false;
  }

  public boolean onInfo(MediaPlayer mp, int whatInfo, int extra) {
    statusView.setText("MediaPlayer onInfo Called");
    if (whatInfo == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) {
      Log.v("", "Media Info, Media Info Bad Interleaving " + extra);
    } else if (whatInfo == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) {
      Log.v("", "Media Info, Media Info Not Seekable " + extra);
    } else if (whatInfo == MediaPlayer.MEDIA_INFO_UNKNOWN) {
      Log.v("", "Media Info, Media Info Unknown " + extra);
    } else if (whatInfo == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) {
      Log.v("", "MediaInfo, Media Info Video Track Lagging " + extra);
    } else if (whatInfo == MediaPlayer.MEDIA_INFO_METADATA_UPDATE) { 
      Log.v("","MediaInfo, Media Info Metadata Update " + extra); }
    return false;
  }

  public void onPrepared(MediaPlayer mp) {
    statusView.setText("MediaPlayer Prepared");
    videoWidth = mp.getVideoWidth();
    videoHeight = mp.getVideoHeight();
    if (videoWidth > currentDisplay.getWidth()
        || videoHeight > currentDisplay.getHeight()) {
      float heightRatio = (float) videoHeight
          / (float) currentDisplay.getHeight();
      float widthRatio = (float) videoWidth
          / (float) currentDisplay.getWidth();
      if (heightRatio > 1 || widthRatio > 1) {
        if (heightRatio > widthRatio) {
          videoHeight = (int) Math.ceil((float) videoHeight
              / (float) heightRatio);
          videoWidth = (int) Math.ceil((float) videoWidth
              / (float) heightRatio);
        } else {
          videoHeight = (int) Math.ceil((float) videoHeight
              / (float) widthRatio);
          videoWidth = (int) Math.ceil((float) videoWidth
              / (float) widthRatio);
        }
      }
    }

    surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
        videoHeight));
    controller.setMediaPlayer(this);
    controller.setAnchorView(this.findViewById(R.id.MainView));
    controller.setEnabled(true);
    controller.show();

    mp.start();
    statusView.setText("MediaPlayer Started");
  }

  public void onSeekComplete(MediaPlayer mp) {
  }

  public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    videoWidth = mp.getVideoWidth();
    videoHeight = mp.getVideoHeight();
    if (videoWidth > currentDisplay.getWidth()
        || videoHeight > currentDisplay.getHeight()) {
      float heightRatio = (float) videoHeight
          / (float) currentDisplay.getHeight();
      float widthRatio = (float) videoWidth
          / (float) currentDisplay.getWidth();

      if (heightRatio > 1 || widthRatio > 1) {
        if (heightRatio > widthRatio) {
          videoHeight = (int) Math.ceil((float) videoHeight
              / (float) heightRatio);
          videoWidth = (int) Math.ceil((float) videoWidth
              / (float) heightRatio);
        } else {
          videoHeight = (int) Math.ceil((float) videoHeight
              / (float) widthRatio);
          videoWidth = (int) Math.ceil((float) videoWidth
              / (float) widthRatio);
        }
      }
    }

    surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
        videoHeight));
  }

  public void onBufferingUpdate(MediaPlayer mp, int bufferedPercent) {
    statusView.setText("MediaPlayer Buffering: " + bufferedPercent + "%");
  }
  public boolean canPause() {
    return true;
  }
  public boolean canSeekBackward() {
    return true;
  }
  public boolean canSeekForward() {
    return true;
  }
  public int getBufferPercentage() {
    return 0;
  }
  public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
  }

  public int getDuration() {
    return mediaPlayer.getDuration();
  }
  public boolean isPlaying() {
    return mediaPlayer.isPlaying();
  }
  public void pause() {
    if (mediaPlayer.isPlaying()) {
      mediaPlayer.pause();
    }
  }
  public void seekTo(int pos) {
    mediaPlayer.seekTo(pos);
  }
  public void start() {
    mediaPlayer.start();
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (controller.isShowing()) {
      controller.hide();
    } else {
      controller.show();
    }
    return false;
  }
}

//main.xml

<?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"
    android:id="@+id/MainView"
    >
  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/StatusTextView" android:text="@+id/Status"></TextView>
  <SurfaceView android:id="@+id/SurfaceView" android:layout_height="200dip" android:layout_width="200dip"></SurfaceView>
</LinearLayout>

   
    
  








Related examples in the same category

1.Record video
2.Capture Video
3.Store image and video
4.Load video file from local file system
5.Uri for local video file
6.Custom Video Player
7.Get Video size
8.Display video with VideoView
9.Using MediaController to control Video
10.Video Gallery
11.View Video with VideoView
12.Video Capture
13.Video Capture with Metadata
14.VideoView Demo