Android : How to set MediaPlayer volume programmatically?

后端 未结 10 523
你的背包
你的背包 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:39

    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
    

提交回复
热议问题