2013-08-06 54 views
0

我的getVolume()方法总是返回1.0。我已经静音了我的电话,改变了音量,但是当我打电话给我的方法时,我随时都可以听到。我错过了什么吗?AudioManager getVolume自定义方法

btnPlay.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     float volume = getVolume(); 
     Log.i("inf", "Volume: " + volume); 
     soundPlayID = soundPool.play(my_sound, volume, volume, 1, 0, 1f); 
    } 
}); 

private float getVolume(){ 
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    float volume = actualVolume/maxVolume; 

    return volume; 
} 

控制台:

体积:1.0

体积:1.0

体积:1.0

体积:1.0

回答

2

Ť他在这里maxVolume看起来与音量相同。这些调用是相同的,所以你总是会得到1.0。

你需要调用:

audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

,然后你会看到更有趣的结果。