Get Video size : Video « Media « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Media » Video 
Get Video size
  

package app.test;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.widget.LinearLayout;

import android.media.MediaPlayer;
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.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import android.widget.MediaController;

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

  SurfaceView surfaceView;
  SurfaceHolder surfaceHolder;

  MediaPlayer mediaPlayer;
    MediaController controller;

  int videoWidth = 0, videoHeight = 0;

  boolean readyToPlay = false;

  public final static String LOGTAG = "CUSTOM_VIDEO_PLAYER";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    surfaceView = (SurfaceViewthis.findViewById(R.id.SurfaceView);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnVideoSizeChangedListener(this);

    String filePath = Environment.getExternalStorageDirectory().getPath()
        "/Test.m4v";

    try {
      mediaPlayer.setDataSource(filePath);
    catch (Exception e) {
      finish();
    }
      controller = new MediaController(this);
    currentDisplay = getWindowManager().getDefaultDisplay();
  }
  public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer.setDisplay(holder);
    try {
      mediaPlayer.prepare();
    catch (Exception e) {
      finish();
    }
  }
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
  }
  public void surfaceDestroyed(SurfaceHolder holder) {
  }
  public void onCompletion(MediaPlayer mp) {
    finish();
  }
  public boolean onError(MediaPlayer mp, int whatError, int extra) {
    if (whatError == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
      Log.v(LOGTAG, "Media Error, Server Died " + extra);
    else if (whatError == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
      Log.v(LOGTAG, "Media Error, Error Unknown " + extra);
    }

    return false;
  }

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

  public void onPrepared(MediaPlayer mp) {
    videoWidth = mp.getVideoWidth();
    videoHeight = mp.getVideoHeight();
    if (videoWidth > currentDisplay.getWidth()
        || videoHeight > currentDisplay.getHeight()) {
      float heightRatio = (floatvideoHeight
          (floatcurrentDisplay.getHeight();
      float widthRatio = (floatvideoWidth
          (floatcurrentDisplay.getWidth();

      if (heightRatio > || widthRatio > 1) {
        if (heightRatio > widthRatio) {
          videoHeight = (intMath.ceil((floatvideoHeight
              (floatheightRatio);
          videoWidth = (intMath.ceil((floatvideoWidth
              (floatheightRatio);
        else {
          videoHeight = (intMath.ceil((floatvideoHeight
              (floatwidthRatio);
          videoWidth = (intMath.ceil((floatvideoWidth
              (floatwidthRatio);
        }
      }
    }

    surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
        videoHeight));
    mp.start();
    
    controller.setMediaPlayer(this);
      controller.setAnchorView(this.findViewById(R.id.MainView));
      controller.setEnabled(true);
      controller.show();    
  }
  public void onSeekComplete(MediaPlayer mp) {
  }

  public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  }
  
  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"
    >
<SurfaceView android:id="@+id/SurfaceView" android:layout_height="wrap_content" android:layout_width="wrap_content"></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.Display video with VideoView
8.Using MediaController to control Video
9.Play video from Youtube.com
10.Video Gallery
11.View Video with VideoView
12.Video Capture
13.Video Capture with Metadata
14.VideoView Demo
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.