views:

112

answers:

1

Right now I have two buttons. Each one needs to produce a different sound. In the future, there will probably be about 8 buttons, but for now just two.

public class MyActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final Button btnDrum1 = (Button) findViewById(R.id.btnDrum1);
            btnDrum1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                     MediaPlayer mp = MediaPlayer.create(this, R.raw.drum1);
                        mp.start();
                        mp.release();
                }
            });

            final Button btnCym1 = (Button) findViewById(R.id.btnCym1);
            btnCym1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                     MediaPlayer mp = MediaPlayer.create(this, R.raw.cym1);
                            mp.start();
                            mp.release();
                }
            });

        }
    }

Originally i didn't have mp.release() and it would play the sound properly, but eventually the app would crash due to running out of memory. Now with the mp.release() it doesn't crash, but sometimes it doesn't play the sound when clicked.

Is this the most efficeient way to play a sound when button is clicked? Is it extensible?

+1  A: 

I think this is due to you releasing it while it is playing. Make a global MediaPlayer for each sound and use it over and over again, release when the activity is closed (maybe even when it is paused, and reload on resume if the sound files are big). Also, since you will have many buttons, you could have a single onclicklistener on all buttons that you instantiate on onCreate():

private class MyMagicalOnClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.button1:
            //play sound 1
            break;
        case R.id.button2:
            //play sound 2
            break;
        }
    }
}

just comment and tell me if you are unsure on how to implement this :)

edit: Per request, here is a neat implementation that should work wonders with your app. I made it so the activity implements onclicklistener instead, I think it is a bit more clean.

public class Bluarg extends Activity implements OnClickListener{

    MediaPlayer mp1;
    MediaPlayer mp2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mp1 = MediaPlayer.create(this, R.raw.sound1);
        mp2 = MediaPlayer.create(this, R.raw.sound2);

        final Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);

        final Button button2 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.button1:
            mp1.start();
            break;
        case R.id.button2:
            mp2.start();
            break;
        }
    }

    @Override
    protected void onDestroy() {
        mp1.release();
        mp2.release();
        super.onDestroy();
    }
}
sandis
What you are describing seems to make sense, but I am a little unsure of the best way to implement that.
Bromide
there, I edited in an implementation. Just make sure to understand what I have done so you can use this in other situations as well :)
sandis