Example usage for android.view SurfaceHolder.Callback SurfaceHolder.Callback

List of usage examples for android.view SurfaceHolder.Callback SurfaceHolder.Callback

Introduction

In this page you can find the example usage for android.view SurfaceHolder.Callback SurfaceHolder.Callback.

Prototype

SurfaceHolder.Callback

Source Link

Usage

From source file:uk.ac.hutton.ics.buntata.activity.VideoActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (player == null) {
        try {/*from w ww .ja  v a  2 s .c om*/
            player = new MediaPlayer();
            player.setDataSource(this, uri);

            mediaController = new MediaController(this);

            sv.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    player.setDisplay(holder);
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    player.setDisplay(holder);
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
            });

            player.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                    // // Get the dimensions of the video
                    int videoWidth = player.getVideoWidth();
                    int videoHeight = player.getVideoHeight();
                    float videoProportion = (float) videoWidth / (float) videoHeight;

                    // Get the width of the screen
                    Point point = new Point();
                    getWindowManager().getDefaultDisplay().getSize(point);
                    int screenWidth = point.x;
                    int screenHeight = point.y;
                    float screenProportion = (float) screenWidth / (float) screenHeight;

                    // Get the SurfaceView layout parameters
                    ViewGroup.LayoutParams lp = sv.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
                    sv.setLayoutParams(lp);

                    player.start();
                }
            });
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaController.setMediaPlayer(VideoActivity.this);
                    mediaController.setAnchorView(sv);
                }
            });
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}