Created
September 23, 2021 07:06
-
-
Save coderspacedev/4643ad59e09de0aecfdba85cb0266f73 to your computer and use it in GitHub Desktop.
Play video from raw folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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