Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to set Button Click sound to be controlled by Android system AudioManager

Posted on November 14, 2022

Solution 1 :

I’ve got the answer.
I have taken my time to understand that setting the sound in a button under onClickListener by calling mp.start only do not keep the user settings. I also discovered that clicking the button repeatedly secretly raises the audiomanager volume without user consent.

So to have the button click sound play under user set system volume i decided to place a constant setting switching affects which helps to keep to users constant settings.

Now if the user turn down the system volume or switch to silent mode the button click sound will not make a sound.

Below is most correct way of implementing button-click sound code

public void onBtnClick()
{
    Log.v(TAG, "Initializing sounds...");
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_btn);

    myButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.playSoundEffect(SoundEffectConstants.CLICK);
            mBrandNewDesc2.setVisibility((mBrandNewDesc2.getVisibility() == View.VISIBLE)
                    ? View.GONE : View.VISIBLE);
            Log.v(TAG, "Playing sound...");
            mp.start();

        }
    });

Problem :

I’m wondering if there is something I should do to have my button click controlled by Android MediaPlayer.
I discovered that even when the phone is set to vibration or silent mode, and the button is clicked it’s still make uncontrolled sound.

public void onBtnClick()
    {
        Log.v(TAG, "Initializing sounds...");
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_btn3);

        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mBrandNewDesc2.setVisibility((mBrandNewDesc2.getVisibility() == View.VISIBLE)
                        ? View.GONE : View.VISIBLE);
                Log.v(TAG, "Playing sound...");
                mp.start();

            }
        });

This is what i did that didn’t work

Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 1);
        v.playSoundEffect(SoundEffectConstants.CLICK);

Comments

Comment posted by Joseph

Please this mediaPlayer does not have contol with respect to OS mediaplayer control

Comment posted by Joseph

I mean how to integrate this mediaplayer above with android manager so as to have the volume controlled by systemservice

READ  [ANSWERED] kotlin - Android Camera2 recorded video always showing 30fps frame rate
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress