get Fit Size for MediaPlayer - Android Media

Android examples for Media:Media Player

Description

get Fit Size for MediaPlayer

Demo Code


import android.content.Context;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class Main{
    private static final String TAG = "CommonUtil";
    public static ViewSize getFitSize(Context context,
            MediaPlayer mediaPlayer) {/*from   w w  w.  j a  va  2 s . c o m*/
        int videoWidth = mediaPlayer.getVideoWidth();
        int videoHeight = mediaPlayer.getVideoHeight();
        double fit1 = videoWidth * 1.0 / videoHeight;

        int width2 = getScreenWidth(context);
        int height2 = getScreenHeight(context);
        double fit2 = width2 * 1.0 / height2;

        Log.e(TAG, "videoWidth = " + videoWidth + ", videoHeight = "
                + videoHeight + ",fit1 = " + fit1);
        Log.e(TAG, "width2 = " + width2 + ", height2 = " + height2
                + ",fit2 = " + fit2);

        double fit = 1;
        if (fit1 > fit2) {
            fit = width2 * 1.0 / videoWidth;
        } else {
            fit = height2 * 1.0 / videoHeight;
        }

        Log.d(TAG, "fit = " + fit);

        ViewSize viewSize = new ViewSize();
        viewSize.width = (int) (fit * videoWidth);
        viewSize.height = (int) (fit * videoHeight);

        return viewSize;
    }
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }
    public static int getScreenHeight(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }
}

Related Tutorials