按钮播放鼓onClick

时间:2015-06-16 20:18:30

标签: android performance

首先它起作用了一段时间。然后之后没有任何按钮工作。如何保持按钮工作?另外,我如何减少延迟并使其响应更快(更少延迟)

这是我的onCreate活动,我开始一切:

public class Drum extends AppCompatActivity {
    Button bass;
    Button snare;
    Button kick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drum);
        bass = (Button) findViewById(R.id.bass);
        snare = (Button) findViewById(R.id.snare);
        kick = (Button) findViewById(R.id.kick);
        final MediaPlayer mp = new MediaPlayer();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.reset();
                mp.release();

            }
        });
        bass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mp.create(getApplicationContext(), R.raw.bass).start();
                mp.release();

                //sounds.play(bassID, 100, 100, 1, 0, 1f);
              //  bassplay.reset();

            }
        });
        snare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //  SoundUtility.PlaySound(mp, R.raw.snares, getApplicationContext());
                mp.create(getApplicationContext(), R.raw.snare).start();


                //snareplay.start(); // no need to call prepare(); create() does that for you
                //snareplay.reset();
            }
        });
        kick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.create(getApplicationContext(), R.raw.kick).start();
                //kickplay.start(); // no need to call prepare(); create() does that for you
                //kickplay.reset();

            }
        });
    } //END OF ONCREATE

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_drum, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}//end of activity

1 个答案:

答案 0 :(得分:1)

对于音效,请使用SoundPool。在同时或连续播放声音片段时效率更高。