简体   繁体   中英

MediaPlayer.Volume control

I have a slider where I get a value from 0 to 100 to adjust the volume. The MediaPlayer.Volume expects a value between 0.0 and 1.0 while 0=-96db and 1=-0db. Currently I use the following code to adjust the linear values from my slider:

 float newVolume = (float)(Math.Sqrt(sliderValue) / 10);
 newVolume = MathHelper.Clamp(newVolume, 0.0001f, 1.0f);
 MediaPlayer.Volume = newVolume;

This works better then directly mapping the 0 to 100 values but in the higher half the adjustment is still rather low compared to the lower half. Are there any better solutions?

This works for me, where 'value' is from 0 to 100

        value = MathHelper.Clamp(value, 0f, 100f);
        var logged = Math.Log(value + 1) / Math.Log(101);
        MediaPlayer.Volume = MathHelper.Clamp((float)logged, 0.001f, 1.0f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM