'how can i create a stop button in android studio for a audio player

Imma student in IT college and i got a task to make an music player on android, my "track1" is playing, but the second one not, and i need to do a pause button. Thanks in advance

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    Button click = (Button) findViewById(R.id.button1);
    Button click1 = (Button) findViewById(R.id.button2);


    final MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.track1);
    final MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(), R.raw.track2);
    View.OnClickListener elem = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.button1:
                    mp1.start();
                    break;
                case R.id.button2:
                    mp2.start();
                    break;

            }
        }
    };

    click.setOnClickListener(elem);
}

}



Solution 1:[1]

as you start mp2 call pause or stop on mp1 and as you start mp1 call pause or stop on mp2.

https://developer.android.com/reference/android/media/MediaPlayer#pause()

https://developer.android.com/reference/android/media/MediaPlayer#stop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arno den Hond