Android : How to set MediaPlayer volume programmatically?

后端 未结 10 515
你的背包
你的背包 2020-12-08 04:17

How to set the mediaplayer volume programmatically. I use it for alarm notification. Any help is highly appreciated and thanks in advance.

相关标签:
10条回答
  • 2020-12-08 04:43

    The below code sets the volume to the maximum level (getStreamMaxVolume()).

    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
    
    0 讨论(0)
  • 2020-12-08 04:44

    Using AudioManager, you can simply control the volume of media players.

    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
    

    also from MediaPlayer (But I didn't try that)

    setVolume(float leftVolume, float rightVolume)
    

    Since: API Level 1

    Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.

    Parameters

    leftVolume left volume scalar

    rightVolume right volume scalar

    0 讨论(0)
  • 2020-12-08 04:52

    Hope this help

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

    For Volume UP

     audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                            AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
    

    for Volume Down

    audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                            AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
    
    0 讨论(0)
  • 2020-12-08 04:52

    To Hide Volume Controll UI:

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

    For Volume UP

    audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_RAISE, 0);
    

    for Volume Down

    audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_LOWER, 0);
    
    0 讨论(0)
提交回复
热议问题