Wednesday, 27 June 2012

ANDROID - Sound & Shiver

There are these few times when you really wanna give the app some cool features which can be as simple as putting some sounds on button clicks ! This is by far the easiest way to do so -

 public void onClick(View v) {
  MediaPlayer mp = MediaPlayer.create(TestSonido.this, R.raw.sound);  

where R.raw.sound => sound.mp3 in the directory - res/raw

Now as for shivers, if you want to make the phone vibrate on simple clicks do this -
In onCreate 
Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE) ;

Then in the OnClickListener of your button:
vibe.vibrate(50); // 50 is time in ms

And dont forget you need to add the permission to the manifest (after the </application> tag):
<uses-permission android:name="android.permission.VIBRATE" />

No comments:

Post a Comment

Feel free to contact us via our website -