final MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("file:///sdcard/video.mp4"); mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() { @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { Log.d("MediaPlayer", "Video size changed: " + width + "x" + height); } }); mediaPlayer.prepare(); mediaPlayer.start();
import android.media.MediaPlayer; import android.widget.VideoView; VideoView videoView = findViewById(R.id.videoView); videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video)); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() { @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { videoView.setMediaController(new MediaController(MainActivity.this)); videoView.requestFocus(); videoView.start(); } }); } });In this example, we create a VideoView instance and set the video URI to a raw resource file. We then register an OnPreparedListener and override the onPrepared() method. When the media player is prepared, we register an OnVideoSizeChangedListener inside the onPrepared() method. When the video size changes, we create a MediaController instance and set it to the VideoView, set focus to the VideoView, and start the video. Both examples belong to the android.media package library.