/*
========================================================================================
Filename: 10_PlayAudioVideo.txt
Program 10: Create an app that plays audio when we click on the button "Play Music" and plays a video when we click on the button "Play Video".
Files Included: MainActivity.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   Add 1 TextView:
     - tvTitle: 'Video Audio Player'
   Add 1 VideoView:
     - vvPlayer: For video playback with MediaController
   Add 2 Button:
     - Text: 'Play Video', onClick: 'toggleVideo'
     - Text: 'Play Audio', onClick: 'toggleAudio'

2. Layout Configuration:
   - LinearLayout (vertical, gravity='center', padding 20dp)

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Default theme. No changes needed.

5. AndroidManifest.xml:
   <activity android:name=".MainActivity" android:exported="true" />

6. Additional Files & Resources Required:
   - Create folder 'res/raw/' and add 'audio.mp3' (or 'audio') and 'video.mp4' (or 'video').
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.playaudiovideo;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer;
    VideoView vvPlayer;
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);
        vvPlayer=findViewById(R.id.vvPlayer);
        vvPlayer.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video);
        mediaPlayer=MediaPlayer.create(this,R.raw.audio);
    }

    public void toggleVideo(View v) {
        Button b=(Button)v;
        if(vvPlayer.isPlaying()) {
            vvPlayer.pause();
            b.setText("Play Video");
        }

        else {
            vvPlayer.start();
            b.setText("Pause Video");
        }
    }

    public void toggleAudio(View v) {
        Button b=(Button)v;
        if(mediaPlayer!=null) {
            if(mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                b.setText("Play Audio");
            }

            else {
                mediaPlayer.start();
                b.setText("Pause Audio");
            }
        }
    }

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

        super.onDestroy();
    }
}
