Skip to content

Instantly share code, notes, and snippets.

@coderspacedev
Created September 23, 2021 07:06
Show Gist options
  • Save coderspacedev/4643ad59e09de0aecfdba85cb0266f73 to your computer and use it in GitHub Desktop.
Save coderspacedev/4643ad59e09de0aecfdba85cb0266f73 to your computer and use it in GitHub Desktop.
Play video from raw folder
private void playVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.video;
binding.videoView.setVideoURI(Uri.parse(path));
binding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
binding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
float videoRatio = mp.getVideoWidth() / ((float) mp.getVideoHeight());
float screenRatio = mp.getVideoWidth() / ((float) mp.getVideoHeight());
float scaleX = videoRatio / screenRatio;
if (scaleX >= 1f) {
binding.videoView.setScaleX(scaleX);
} else {
binding.videoView.setScaleY(1f / scaleX);
}
binding.videoView.setBackgroundColor(Color.TRANSPARENT);
binding.videoView.start();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment