Android : How to set MediaPlayer volume programmat

2019-01-16 23:17发布

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

10条回答
Bombasti
2楼-- · 2019-01-16 23:42

Code:

AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
try 
 {        
   float count = 100 * 0.2f;
   Log.d("--count_float", count + "");
   Log.d("--count_final", Math.round(count) + "");
   Log.d("--count_volume", new 
   PreferenceMotionSensor(mContext).getStreamVolume());
   mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Math.round(count), 0);
 } 
catch (Exception e) 
 {
  Log.d("--Error", e.getMessage());
 }

Output

D/--count_float: 20.0
D/--count_final: 20
D/--count_volume: 100
查看更多
男人必须洒脱
3楼-- · 2019-01-16 23: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);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-16 23:46

Using AudioManager you can control the volume of media player.

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

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

public void  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

查看更多
相关推荐>>
5楼-- · 2019-01-16 23:48

Try This

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();
            }
        }

    }
查看更多
smile是对你的礼貌
6楼-- · 2019-01-16 23:57

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);
查看更多
干净又极端
7楼-- · 2019-01-16 23:57

Read this page right here. It explains very well. Basically, unless your app is a replacement alarm clock, you need to make the following call in the "onCreate()" function:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

In this way you can create the volume of your app using the hardware buttons.

查看更多
登录 后发表回答