On and off buttons not working properly - android

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

Related

Click sound not playing android onClick

I have this:
android:onClick="onClick"
android:soundEffectsEnabled="true"
in my XML. And in my class I have:
public void onClick(View v) {
v.playSoundEffect(SoundEffectConstants.CLICK);
increment(button, key, this);
}
However the CLICK sound is still not working
According with Android developer page the method PlaySoundEffect
The sound effect will only be played if sound effects are enabled by the user, and isSoundEffectsEnabled() is true.
So in the Settings-> Sounds the sound effects must be activated.
Try
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),<yoursound> );
mp.start();
instead.
This sound will play always if audio (for media) is turned on

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

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.

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 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.

Play default soft keyboard sound when buttons are pressed in android

I have developed an app which uses my own custom keyboard (well, a view that looks like a keyboard and behaves like a keyboard anyway). One thing I've yet to figure it out is how to make it play the default soft keyboard 'click' sound when the buttons are pressed.
Is there any easy way to do this?
I would like to use the keyboard click sound that comes with the phone rather than providing my own. As different phones might have different keyboard click sounds, I would like to keep my application consistent. Ultimately, I want to reflect the same settings the user has chosen in their global keyboard settings (play/not play sounds, vibrate/not vibrate, etc).
I have found a solution to this. All I needed to do was implement a OnTouchListener on the button and use the AudioManager.playSoundEffect() public method. Code is shown below:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float vol = 0.5; //This will be half of the default system sound
am.playSoundEffect(AudioManager.FX_KEY_CLICK, vol);
if (isSetVibration) {
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(50);
}
} else {
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(0, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(0);
}
}

Categories

Resources