Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myvideoviewplayer;
- import androidx.appcompat.app.AppCompatActivity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.VideoView;
- public class MainActivity extends AppCompatActivity {
- private VideoView mVideoView;
- private Button btnPlay, btnStop;
- private Uri videoUri;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mVideoView = findViewById(R.id.vvVideoView);
- btnPlay = findViewById(R.id.btnPlay);
- btnStop = findViewById(R.id.btnStop);
- initializeVideo();
- btnPlay.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startVideo();
- }
- });
- btnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- stopVideo();
- }
- });
- }
- private void initializeVideo() {
- String mediaName = "video_file_example"; // file name from raw folder
- String uriString = "android.resource://" + getPackageName() + "/raw/" + mediaName;
- videoUri = Uri.parse(uriString);
- mVideoView.setVideoURI(videoUri);
- }
- private void startVideo() {
- if (!mVideoView.isPlaying()) {
- mVideoView.setVideoURI(videoUri);
- mVideoView.start();
- btnPlay.setEnabled(false);
- btnStop.setEnabled(true);
- }
- }
- private void stopVideo() {
- if (mVideoView.isPlaying()) {
- mVideoView.stopPlayback();
- btnPlay.setEnabled(true);
- btnStop.setEnabled(false);
- }
- }
- @Override
- protected void onPause() {
- stopVideo();
- super.onPause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- startVideo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement