Click sound not playing android onClick - android

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

Related

How to play two sounds at the same time?

I've been trying a few things with sounds in flutter lately . Using audioPlayer plugin.
But I'm facing a wall. I have both a background music, and small sounds effects.
The problem is that I can't play both at once. If I play the background music, then the sound effect won't play. And the opposite works too.
Any idea about how to solve that issue ?
This is an issue with the audioplayer plugin and there's an issue open for it already. The author has indicated that he's open to pull requests if you'd like to take a crack at implementing it.
Use media players. Use this code to set up the background music:
MediaPlayer backgroundMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_BACKGROUND_MUSIC));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_BACKGROUND_MUSIC with the background music path.
Then when you want your sound effect to happen, use this code:
MediaPlayer soundEffectMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_SOUND_EFFECT));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_SOUND_EFFECT with the background music path.
You can learn for about media players here.
Hope this helps!

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 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 a melody on click

When I click on a view, I want to play a melody. The problem is that when I click very fast, the melody starts from the beginning and I want it to play to the end and at this time screen must not react to clicks (because I don't want the melody to be played 10 times if I clicked very fast 10 times). I wrote something like this:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
mp.start();
if(mp.isPlaying()) {
img1.setClickable(false);
}
img1.setClickable(true);
But after one click the screen does not react to click anymore... Help me please with this.
//Init your MediaPlayer when Activity is created, for instance:
MediaPlayer mp = MediaPlayer.create(MyActivity.this, R.raw.cow_moo);
//**************************//
//onClickListener goes here:
if(mp.isPlaying()){
//do nothing
}else{
mp.start();
}
//onClickListener ends here:
Read the android developers guide, it says "If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again"
Upon click event check if the media player has finished its playback. If no, then do nothing, if yes then play again.
To check if finished use this method below:
isPlaying()
You may check this:
http://developer.android.com/reference/android/media/MediaPlayer.html
Hope you find it easy. :)

Categories

Resources