Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 909
Ricta Scott Avatar asked Dec 03 '12 09:12

Ricta Scott


2 Answers

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!

like image 23
Bart Burg Avatar answered Sep 29 '22 05:09

Bart Burg


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

like image 73
ρяσѕρєя K Avatar answered Sep 29 '22 07:09

ρяσѕρєя K