Home » Java » java – Android Studio Soundboard: How to start a loop with a mediaplayer button?-Exceptionshub

java – Android Studio Soundboard: How to start a loop with a mediaplayer button?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to use a switch to turn on or off a loop for each button when it plays its sounds.
When the switch is toggled, i want any button i press after that to loop until the switch is toggled off.

I have about 30 buttons and they all have their own setOnClickListener with an mp3 in raw folder.
Initially, i was using soundPool but it stopped working so I switched to MediaPlayer but I haven’t had any real success.

My latest change was to use Try and Catch instead of using if but I don’t know if it really helped.

package com.goatman.soundboard;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends Activity {
public static MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playBruh1 = (Button) findViewById(R.id.bruhonebutton);
Button playBruh2 = (Button) findViewById(R.id.bruhtwobutton);
Button playBruh3 = (Button) findViewById(R.id.bruhthreebutton);
Button playNani = (Button) findViewById(R.id.nanibutton);
Button playYes = (Button) findViewById(R.id.yesbutton);
Button playAyaya = (Button) findViewById(R.id.ayayabutton);
Button playTop5BruhMoments = (Button) findViewById(R.id.topfivebruhmomentsbutton);
Button playTop10NWords = (Button) findViewById(R.id.toptennwordsbutton);
Button playTop10FunnyWoman = (Button) findViewById(R.id.toptenfunnywomanbutton);
Button playArab1 = (Button) findViewById(R.id.arabonebutton);
Button playArab2 = (Button) findViewById(R.id.arabtwobutton);
Button playArab3 = (Button) findViewById(R.id.arabthreebutton);
Button playHell1 = (Button) findViewById(R.id.hellonebutton);
Button playHell2 = (Button) findViewById(R.id.helltwobutton);
Button playHell3 = (Button) findViewById(R.id.hellthreebutton);
Button playNWord1 = (Button) findViewById(R.id.nwordonebutton);
Button playNWord2 = (Button) findViewById(R.id.nwordtwobutton);
Button playNWord3 = (Button) findViewById(R.id.nwordthreebutton);
Button playWindows1 = (Button) findViewById(R.id.windowsonebutton);
Button playWindows2 = (Button) findViewById(R.id.windowstwobutton);
Button playWindows3 = (Button) findViewById(R.id.windowsthreebutton);
Button playGun1 = (Button) findViewById(R.id.gunonebutton);
Button playGun2 = (Button) findViewById(R.id.guntwobutton);
Button playGun3 = (Button) findViewById(R.id.gunthreebutton);
Button playFrench1 = (Button) findViewById(R.id.frenchonebutton);
Button playFrench2 = (Button) findViewById(R.id.frenchtwobutton);
Button playFrench3 = (Button) findViewById(R.id.frenchthreebutton);
Button playCBT = (Button) findViewById(R.id.cbtbutton);
Button playChicken = (Button) findViewById(R.id.chickenbutton);
Switch playLoop = (Switch) findViewById(R.id.loop);
playLoop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.setLooping(true);
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp.start();
}
});
playBruh1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.bruh1);
mp.start();
}
});
playBruh2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.bruh2);
mp.start();
}
});
playBruh3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.bruh3);
mp.start();
}
});
playArab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab1);
mp.start();
}
});
playArab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab2);
mp.start();
}
});
playArab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab3);
mp.start();
}
});
playNani.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.nani);
mp.start();
}
});
playYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.yes);
mp.start();
}
});
playAyaya.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.ayaya);
mp.start();
}
});
playTop5BruhMoments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.top5bruhmoments);
mp.start();
}
});
playTop10NWords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.top10nwords);
mp.start();
}
});
playTop10FunnyWoman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.top10funnywoman);
mp.start();
}
});
playHell1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab2);
mp.start();
}
});
playHell2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.hell2);
mp.start();
}
});
playHell3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.hell3);
mp.start();
}
});
playNWord1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.nword1);
mp.start();
}
});
playNWord2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.nword2);
mp.start();
}
});
playNWord3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.nword3);
mp.start();
}
});
playWindows1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.windows1);
mp.start();
}
});
playWindows2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.windows2);
mp.start();
}
});
playWindows3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab2);
mp.start();
}
});
playGun1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.gun1);
mp.start();
}
});
playGun2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.gun2);
mp.start();
}
});
playGun3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.gun3);
mp.start();
}
});
playFrench1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.french1);
mp.start();
}
});
playFrench2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.french2);
mp.start();
}
});
playFrench3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.arab2);
mp.start();
}
});
playCBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.cbt);
mp.start();
}
});
playChicken.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
mp = MediaPlayer.create(MainActivity.this, R.raw.chicken);
mp.start();
}
});
}
}
How to&Answers:

mp.setLooping(true);

Call this before starting a new sound everytime.
I hope this helps 😀