How to disable vibration and sound on touch in Android programmatically? - android

I am making one application and opening alertdialog using OnLongClickListener of ImageView and also ontouch zooming. But when I pressed long than vibrating on so is it possible to turn off vibrating on long click listener. I am using below code:
public OnLongClickListener longClick = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Vibrator vibe = (Vibrator)DailySatsangActivity.this.getSystemService(Context.VIBRATOR_SERVICE) ;
vibe.cancel();
}
};

I think the problem you are facing is Haptic Feedback instead of the normal vibration.
You can turn-off "Haptic Feedback" by using below code,
XML attribute: android:hapticFeedbackEnabled //set it true or false in view's xml file for the desired result.
Java method: setHapticFeedbackEnabled(boolean)
OR
You can use Audio Manger class to disable sound and vibration, have a look at setRingerMode method of Audio Manager class.
Example :
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setRingerMode(aManager.RINGER_MODE_SILENT);
Hope this will solve your problem.

You should use AudioManager class for this.

Related

On and off buttons not working properly

I coded an Android app, in which it has two buttons, one for mute the music_player and other for unmute it. But when I press the mute button two times in order to increase the media volume I need to press unmute button two times. How to avoid this? I want to unmute the media volume no matter how many times I mute the media volume. I used the following code:
Mute:
AudioManager am = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
Unmute:
AudioManager am = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
Editor's note: The two code segments above are identical. That's probably incorrect.
You may specify a boolean flag isMute and use it to toggle mute by one button just like this:
boolean isMute = false;
Button mMuteButton;
...
mMuteButton.setOnclickListener(
new OnClickListener {
public void onClick(View v) {
AudioManager am = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, !isMute);
isMute = !isMute;
}
});
Or if you extremely want to use two buttons, so just disable one after mute or unmute

Android tutorial Button "Save" the changes in Ringtone, Notification Volume

Doese anyone know of a tutorial on how to make a Volume Controller like this one (with a Save button at the bottom). This is my problem, how to make that "Save" button. I can create all the volume SeekBars, but unfortunately when I make changes on SseekBar it automatically changes the volume from the phone settings (ringer, notification, media, in call).
Thanks in advance.
First off, use the SeekBar and retrieve the value it sets to. Don't set the volumes while the SeekBar is adjusted. And then, when the button Save is pushed, send those values to the AudioManager.
Use this link for more detailed explanation: Android Development: Change Media volume? as well as this one: SeekBar1.java
Here's a quick example of how it might look like:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
ImageButton save = (ImageButton) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, valueFromSeekBar, flagIfNeeded);
}
}); //End setOnClickListener()

How to set up Media controller to play audio?

i have an image with some text that i have the audio for.
I want to know how i can set it up so the user can the the button and the audio will start to play.
Thanks!
I have a solution I think it might work.
Fisrt of all, instead of working it an ImageView would be easier to work with ImageButton.
So, in your layour XML file, select ImageButton and create one using your image with the text.
also you might want to set a null bacground for your ImageButton, this is the code
android:background="#null"
Next, in your java class select the sound and the ImageButton and set it to play your sound when clicked.
would be like this
final MediaPlayer yourSoundName = MediaPlayer.create(Lotr1.this,R.raw.yoursoundfile);
final ImageButton yourImageButtonName =(ImageButton) findViewById(R.id.yourImage);
yourImageButtonName .setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
yourSoundName.start();
}
}
});
Any quenstion I would be glad to help

How to produce default sound

I hava a set of buttons. When I click on a button it should produce sound.
Example:
Button b=new Button(this);
b.setText("Press");
b.setOnClickListener(new OnClickListener)[
public void click(View v)
{
b.setSoundEffectsEnabled(true);
});}
This doesn't work though, can anyone help me please.
What do you mean on default sound? If you want to play your own sound, you must create a MediaPlayer like this.
MediaPlayer mediaPlayer = MediaPlayer.create(this, [here is your sound in the raw file]);
and in the click method you need to implement this:
mediaPlayer.start();
or you can use soundpool too.
Hope it helps.
You can also use the build in sound notifications
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, ToneGenerator.MAX_VOLUME);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
If you want to play the default click sound when you click on the button, then setting b.setSoundEffectsEnabled(true) should work (although it doesn't need to be on the listener), but it is dependent on the device option to play audible selection. Try checking the device's sound settings if it is on.

Pause MediaPlayer when using a Map in android app

I am building a soundboard with about 40 sounds using a map.
(Previous thread)
Does anyone know a good way to pause restart Mediaplayer if the app is moved to the background (incoming call or anything like that)? I'm still very new at this so it is probably something super simple. Thanks anyone who can help.
Map map = new
HashMap();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
...
and then iterate:
for (Map.Entry entry : map.entrySet()) {
final MediaPlayer sound = MediaPlayer.create(entry.getValue());
Button button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.start();
}
});
}
I assume you don't want to play all sounds together, so you can declare the mediaplayer object outside the loop and then you don't need to know which file is being played. You can use sound.pause(); sound.stop(); or sound.reset(); (depends on the action you want to preform) I suggest you look at the android docs to see how to implement it properly.
If you want to create mediaplayer for each file which I doubt you want, save the mediaplayers in a map / list and pause / reset them in loop during onPause().
Override Activity.onPause(). This is called whenever your app goes to the background, Home button or back button or incoming call or magic thunderclouds of doom.

Categories

Resources