0

I'm trying to make a soundboard app, but I'm running into a problem with handling the sound files. More specifically I do not know how to get it so when a button is pressed it plays a corresponding sound back.

I've tried using this way Play sound on button click android

I tried implementing a media player for each sound file and button but the app crashes on start up.

I have 10 buttons with 10 corresponding sound files that are located in the raw file in src/res/raw/

Any idea on how to make it work?

//this is a breaking bad soundboard app, so please excuse the language. Sorry if you      find it offensive
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    whatup = (Button)this.findViewById(R.id.button);
    haveatit = (Button)this.findViewById(R.id.button2);
    hello = (Button)this.findViewById(R.id.button3);
    whining = (Button)this.findViewById(R.id.button7);
    money = (Button)this.findViewById(R.id.button4);
    yeah= (Button)this.findViewById(R.id.button8);
    miserable = (Button)this.findViewById(R.id.button5);
    mother = (Button)this.findViewById(R.id.button9);
    gatorade = (Button)this.findViewById(R.id.button6);
    science = (Button)this.findViewById(R.id.button10);

    whatup.setOnClickListener(this);
    haveatit.setOnClickListener(this);
    hello.setOnClickListener(this);
    whining.setOnClickListener(this);
    money.setOnClickListener(this);
    yeah.setOnClickListener(this);
    miserable.setOnClickListener(this);
    mother.setOnClickListener(this);
    gatorade.setOnClickListener(this);
    science.setOnClickListener(this);

    WhatUp = MediaPlayer.create(MainActivity.this, R.raw.whatupb);
    HaveAtIt = MediaPlayer.create(MainActivity.this, R.raw.haveatit);
    Hello = MediaPlayer.create(MainActivity.this, R.raw.hello);
    Whining = MediaPlayer.create(MainActivity.this, R.raw.stopwhining);
    Money = MediaPlayer.create(MainActivity.this, R.raw.wheresmymoney);
    Yeah = MediaPlayer.create(MainActivity.this, R.raw.yeahb);
    Miserable = MediaPlayer.create(MainActivity.this, R.raw.miserableb);
    Mother = MediaPlayer.create(MainActivity.this, R.raw.motherofgod);
    Gatorade = MediaPlayer.create(MainActivity.this, R.raw.gatorade);
    Science = MediaPlayer.create(MainActivity.this, R.raw.yeahscience);

}

this is for handling when the button is pressed. Obviously there needs to be more, but I was just testing 1 button and it crashes when I try starting it.

  @Override
public void onClick(View view) {
    if(view==whatup)
    {
        WhatUp.start();
    }
}

Log Cat Errors: https://i.stack.imgur.com/Hz4Sz.jpg

Community
  • 1
  • 1
TemporaryFix
  • 2,008
  • 3
  • 30
  • 54

1 Answers1

0

I assume your sounds might be sound_1, sound_2, sound_3, etc. and your buttons are button_1, button_2 and so on..
You should create a loop to get the ids in onCreate method:

for (int i = 0; i < 10; i++) {
    // get the id for buttons
    int btnId = getResources().getIdentifier("button_"+i, "id", getPackageName());
    // get the res for sounds
    int rawId = getResources().getIdentifier("sound_"+i, "raw", getPackageName());
    // set a click listener to all your buttons
    button[i] = (Button) findViewById(id);
    button[i].setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            // create the media player with the raw id
            mp = MediaPlayer.create(MainActivity.this, rawId);
            mp.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    // avoid NullPointerException see the links below
                    mp.release();
                }
            });   
            // play the sound
            mp.start();
         }
    });
}  

Read the topic reference: Using MediaPlayer. You could also get identifer with the context: How to dynamically generate the raw resource identifier in android? and this might help you for explanation of release() method: Play sound on button click - Null pointer exception and read the Releasing the MediaPlayer part from Using MediaPlayer. I'm not sure but it should do the trick..
Good luck.


UPDATE

Change this check if(view==whatup) to if(view.getId() == R.id.button).
In onClick method you need to check the id regarding the view, with getId() method.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • This looks helpful. I just found an article that says I should use a soundpool instead of a media player. Do you know the differences? – TemporaryFix Mar 26 '14 at 01:11
  • I've just read the contrary: http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/ -Awesome! ^^ – Blo Mar 26 '14 at 01:20
  • I switched it over and It still crashes on Start up. Here is my code that I have for onCreate – TemporaryFix Mar 26 '14 at 04:07
  • Add the `LogCat` in your question. – Blo Mar 26 '14 at 04:12
  • Added log cat. I couldn't copy the code over so I just took a screen shot – TemporaryFix Mar 26 '14 at 04:20
  • Line 62 in your activity.. you have a `nullpointerexception`. What's is it in the line? – Blo Mar 26 '14 at 04:22
  • Right at the beginning where I assign listeners to my buttons. It's the first button I assign a listener to whatup.setOnClickListener(this); – TemporaryFix Mar 26 '14 at 04:28
  • Hum.. is it the right id in your layout for findViewById(R.id.button)? Isn't it `button1`? – Blo Mar 26 '14 at 04:29
  • I have no idea. This is my first time coding in Android Studio. It has to be the way I set up the layout. Usually I use Eclipse with the android sdk. So far I am not a fan with android studio. I have encountered a lot of issues with migrating. I think my best bet would be to rewrite the code for the layout. Thanks for the help though! – TemporaryFix Mar 26 '14 at 04:33
  • Well, I think you should have the same structure (http://developer.android.com/sdk/installing/studio-tips.html#Project) as Eclipse, your layout might be in `/res/layout/` – Blo Mar 26 '14 at 04:35