Android: replace sound of onclick

拥有回忆 提交于 2019-12-25 06:38:21

问题


I have this code:

Button boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {

 }
});

when I push my button (in Android 4.0.3) I ear a basic sound (a simply click); Can I replace it with a personal sound? Or I can use only MediaPlayer to play a costum sound?


回答1:


Try this:

public class MyActivity extends Activity {

public MyActivity(Bundle onSavedStateInstance) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero);

    Button zero = (Button)this.findViewById(R.id.btnZero);
    zero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp.start();
        }
    });
}

}




回答2:


You can add sound to your raw folder, initialize it

MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.slow);

and call it where ever required

    mpSplash.start();


来源:https://stackoverflow.com/questions/12142541/android-replace-sound-of-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!