开发者

most efficient way to play a sound when button is clicked

开发者 https://www.devze.com 2023-01-05 23:30 出处:网络
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.

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.bt开发者_如何学编程nDrum1);
            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?


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();
    }
}


If your app will have more buttons, you should use SoundPool instead of MediaPlayer. Because MediaPlayer will ruin your app out of memory then it can't play sound anymore.

This is helped me out ! and I think you will too Play sound with SoundPool

0

精彩评论

暂无评论...
验证码 换一张
取 消