How do I use the default click sound using view.playSoundEffect(SoundEffectConstants.CLICK);

岁酱吖の 提交于 2019-12-07 05:06:23

问题


I wan to use the default TICK sound but I dont know how or where to put this code

view.playSoundEffect(SoundEffectConstants.CLICK);

I found this code in another post.

This is the code I have:

        protected void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.blah);

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

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

    public void onClick(View v){}

    public void disclaimerBTN (View v){
        Toast.makeText(this, "FAILED:      The remote object is " +
                            "not responding to this command",Toast.LENGTH_LONG).show();
    }
}

So where would I put the view.play..... code? Much appreciated.


回答1:


For Using AudioManager.playSoundEffect on Button click event you can try as:

AudioManager audioManager = 
            (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

 Button01.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                 audioManager.playSoundEffect(SoundEffectConstants.CLICK);   
                 //mp.start();
            }
        });

for reference you can see this example on Google source code:

http://code.google.com/p/android-traditional-chinese-ime/source/browse/trunk/src/com/googlecode/tcime/SoundMotionEffect.java?r=13




回答2:


You can play a sound from every view just by calling it this way:

Button01.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                 v.playSoundEffect(SoundEffectConstants.CLICK);   
            }
        });

Just note that the sound will not play if touch sounds are off by default. This is set in the general device sound preferences (Settings-->Sound-->Audible or on newer OS: Options > Sound > Touch)

Also, if this setting is set, most click events will trigger the click sound anyway!



来源:https://stackoverflow.com/questions/13680163/how-do-i-use-the-default-click-sound-using-view-playsoundeffectsoundeffectconst

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