Change media player volume on seekbar

后端 未结 2 928
你的背包
你的背包 2020-12-22 08:51

I am working on a game application. But there is a problem on volume control. I want to change media player volume using a seekBar, and not system media volume. Is there any

相关标签:
2条回答
  • 2020-12-22 09:44

    First, get a reference from AudioManager;

    AudioManager audioMan = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    

    To raise the volume:

     audioMan.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE | AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    

    To low the volume:

    audioMan.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_VIBRATE | AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    

    To get the max volume:

    int maxVolume = audioMan.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    

    To get the current volume:

    int currVolume = audioMan.getStreamVolume(AudioManager.STREAM_MUSIC);
    

    You can remove any of the flags that I've included

    0 讨论(0)
  • 2020-12-22 09:46

    May be this help...

    protected static void setVolume(int volume) {
            currentVolume = volume;
            {
                if (volume == 1) {
                    volume = 2;
                }
                try {
                    float vol = ((float) volume / CONSTANT.SYSTEM_MAX_VOLUME);
                    mediaPlayer.setVolume(vol, vol);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题