Search code examples
androidonclickandroid-audiomanager

Issue with the sound volumes in button press:


I'm developing currently an IME, and I have sound for button click. I have an option in the preferences screen to change the volume of the sounds. The SeekBar values are going from 0.0 to 1.0. Now I try to let the user to configure the volume of the buttons in the preferences screen and later I get this value and save it as mSoundVol parameter. So for the sound of the click I wrote the following method:

 float soundVolume;
 int maxVolume, sound;

        switch (primaryCode) {
            case Keyboard.KEYCODE_DELETE:
                maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_DELETE);
                sound = mAudioManager.FX_KEYPRESS_DELETE;
                Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_DELETE");
                break;
            case ASCII_ENTER:
                maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_RETURN);
                sound = mAudioManager.FX_KEYPRESS_RETURN;
                Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_RETURN");
                break;
            case ASCII_SPACE:
                maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_SPACEBAR);
                sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
                Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_SPACEBAR");
                break;
            default:
                maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_STANDARD);
                sound = mAudioManager.FX_KEYPRESS_STANDARD;
                Log.d(TAG + "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_STANDARD");
        }

        soundVolume = maxVolume * mSoundVol;
        Log.d(TAG+ "-volume", "current max volume: " + maxVolume + " current volume setting: " +mSoundVol * 100 +"%" + " volume result: " + soundVolume);
        mAudioManager.playSoundEffect(sound, soundVolume);

But for some reason this does not change the volume of the sound for the user.

Can some one tell me what am I doing wrong with the AudioManager here?

Thanks.


Solution

  • The solution in my case was this:

     float soundVolume;
            int maxVolume, sound;
    
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE:
                    sound = mAudioManager.FX_KEYPRESS_DELETE;
                    break;
                case ASCII_ENTER:
                    sound = mAudioManager.FX_KEYPRESS_RETURN;
                    break;
                case ASCII_SPACE:
                    sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
                    break;
                default:
                    sound = mAudioManager.FX_KEYPRESS_STANDARD;
                    break;
            }
            maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            soundVolume = maxVolume * mSoundVol;
            mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM , (int)(soundVolume * 1.5) , 0);
            mAudioManager.playSoundEffect(sound);
    

    The important thing was to pass 0 as the last parameter of the setStreamVolume method, that way this action only changes the volume without playing any other sound of showing the volume UI.