Я пытаюсь использовать переключатель для включения или выключения al oop для каждой кнопки, когда она воспроизводит свои звуки. Когда переключатель переключается, я хочу, чтобы любая кнопка, которую я нажимаю после этого, до l oop, пока переключатель не будет выключен.
У меня около 30 кнопок, и у них всех есть свой собственный setOnClickListener с mp3 в папке raw , Изначально я использовал soundPool, но он перестал работать, поэтому я перешел на MediaPlayer, но реального успеха у меня не было.
Моим последним изменением было использование Try and Catch вместо использования if, но я не знаю если это действительно помогло.
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();
}
});
}
}