2016-11-17 61 views
0

我想设置闹钟的音量。我使用这段代码,但是卷似乎没有任何变化,只有日志显示了我设置的卷的值。我应该怎么做才能真正改变音量?更改闹钟的音量

我的代码:

final MediaPlayer mediaPlayer = MediaPlayer.create(this, RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_ALARM)); 

     final AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     final int currentVolume = audio.getStreamVolume(AudioManager.STREAM_ALARM); 
     Log.e("Point_1", "Volume " + currentVolume); 
     audio.setStreamVolume(AudioManager.STREAM_ALARM,0,0); 
     mediaPlayer.start(); 
     new Timer().schedule(new TimerTask() { 
      @Override 
      public void run() { 
       mediaPlayer.stop(); 
       Log.e("Point_1", "Volume_after " + audio.getStreamVolume(AudioManager.STREAM_ALARM)); 
      } 

    }, 5000); 

感谢。

回答

0

使用此代码

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

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 
          [int value], 
          [if desired a flag]); 

你应该使用的AudioManager.STREAM_MUSIC代替AudioManager.STREAM_ALARM

,看看How to correctly set MediaPlayer audio stream type

+0

嘿,谢谢!另外,你能告诉我有什么方法可以将最大音量除以100,然后用它作为setStreamVolume的值吗? – Steve

+0

看到这个http://stackoverflow.com/questions/15670524/how-to-turn-the-volume-to-max-programmatically-on-android – zohreh