MainActivity.java:

package com.example.appone;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    Button btnMusic, btnStopMusic;
    Button btnVideo, btnStopVideo;

    VideoView videoView;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnMusic = findViewById(R.id.btnMusic);
        btnStopMusic = findViewById(R.id.btnStopMusic);

        btnVideo = findViewById(R.id.btnVideo);
        btnStopVideo = findViewById(R.id.btnStopVideo);

        videoView = findViewById(R.id.videoView);

        mediaPlayer = MediaPlayer.create(this, R.raw.music);

        // Play Music
        btnMusic.setOnClickListener(v -> {
            if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }

        });

        // Stop Music
        btnStopMusic.setOnClickListener(v -> {
            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                mediaPlayer.seekTo(0);
            }
        });

        // Play Video
        btnVideo.setOnClickListener(v -> {

            String path =
                    "android.resource://"
                            + getPackageName()
                            + "/"
                            + R.raw.video;

            Uri uri = Uri.parse(path);

            videoView.setVideoURI(uri);

            MediaController controller =
                    new MediaController(this);

            controller.setAnchorView(videoView);

            videoView.setMediaController(controller);

            videoView.start();
        });

        // Stop Video
        btnStopVideo.setOnClickListener(v -> {

            if (videoView != null) {
                videoView.stopPlayback();

                String path =
                        "android.resource://"
                                + getPackageName()
                                + "/"
                                + R.raw.video;

                videoView.setVideoURI(


                        Uri.parse(path));
            }
        });
    }
    // FIX: pause media when the app goes to the background
    @Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
        if (videoView != null && videoView.isPlaying()) {
            videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MEDIA PLAYER APP"
        android:textSize="26sp"
        android:textStyle="bold"
        android:layout_marginBottom="20dp"/>

    <!-- Music Buttons -->
    <Button
        android:id="@+id/btnMusic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PLAY MUSIC"/>
    <Button
        android:id="@+id/btnStopMusic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="STOP MUSIC"
        android:layout_marginTop="10dp"/>

    <!-- Video Buttons -->
    <Button
        android:id="@+id/btnVideo"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PLAY VIDEO"
        android:layout_marginTop="20dp"/>
    <Button
        android:id="@+id/btnStopVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="STOP VIDEO"
        android:layout_marginTop="10dp"/>
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"/>
</LinearLayout>
