简体   繁体   English

int 浮动为 mediaplayer.setVolume()

[英]int to float for mediaplayer.setVolume()

I am using a seekbar to change the volume of my MediaPlayer.我正在使用搜索栏来更改 MediaPlayer 的音量。 The progress level is what I am using which gives a "1 to 100" int.进度级别是我正在使用的,它给出了“1 到 100”的整数。 I need to convert that into the float range of 0.0f to 1.0f.我需要将其转换为 0.0f 到 1.0f 的浮点范围。 What is the correct way of doing that?这样做的正确方法是什么?

Thanks guys多谢你们

float fVal = (float)val / 100; should do the trick.应该做的伎俩。

Very late in the day, but as I was just reading up on this very subject I thought it worth posting an answer that doesn't just perform a straight linear scaling of the slider value 0 - 100 into a float value 0.0 - 1.0 and explain why you should be doing it differently.当天很晚,但是当我刚刚阅读这个主题时,我认为值得发布一个答案,该答案不仅仅是将 slider 值 0 - 100 直接线性缩放为浮点值 0.0 - 1.0 并解释为什么你应该做不同的事情。

So the API documentation for MediaPlayer.setVolume(float, float) states, in passing, that "UI controls should be scaled logarithmically" but doesn't explain why.因此, MediaPlayer.setVolume(float, float)API 文档顺便指出,“UI 控件应该以对数方式缩放”,但没有解释原因。

Sound as we hear it is measured in decibels (db), on a logarithmic scale.我们听到的声音以分贝 (db) 为单位,采用对数刻度。 In simplified terms (over-simplified if you are an audio buff), twice the decibels = twice the volume.简而言之(如果您是音频爱好者,那就过于简化了),两倍分贝 = 两倍音量。 But because the decibel scale is logarithmic, the distance on the scale from (for example) 0 - 3db is bigger than the distance on the scale from 3db to 6db.但是因为分贝刻度是对数的,所以(例如)0 - 3db 刻度上的距离大于 3db 到 6db 刻度上的距离。

The most obvious effect of using linear scaling instead of logarithmic is that the volume with the slider at maximum is much more than twice as loud as the volume at half way, so most of the noticeable change in volume level happens in the lower three quarters (approximately) of the slider range rather than in an (apparently) linear fashion across the full slider range.使用线性缩放而不是对数缩放最明显的效果是,slider 最大的音量是中途音量的两倍多,因此音量水平的大部分明显变化发生在较低的四分之三(大约)slider 范围,而不是在整个 slider 范围内以(显然)线性方式。 And this is why straight linear scaling isn't quite the right way to translate a slider position into a value for the setVolume method.这就是为什么直线缩放不是将 slider position 转换为setVolume方法的值的正确方法。

Here's a simple function that will take your slider value (assumed to lie in the range 0 - 100), convert it into a logarithmic value and scale it:这是一个简单的 function ,它将获取您的 slider 值(假设位于 0 - 100 范围内),将其转换为对数值并对其进行缩放:

private float scaleVolume(int sliderValue) {

    double dSliderValue = sliderValue;
    double logSliderValue = Math.log10(dSliderValue / 10);
    double logMaxSliderValue = Math.log10(10);
    float scaledVolume = (float) (logSliderValue / logMaxSliderValue);

    return scaledVolume;

}

Now, the slider at 50 (the center position) will produce a sound that is about half as loud as when the slider is at 100 (the top position), and the slider at 25 will produce a sound that is half as loud as when the slider is at 50.现在,slider 在 50(中心位置)将产生的声音大约是 slider 在 100(顶部位置)时的一半,而 slider 产生的声音是在 slider 时的一半slider 为 50。

Be aware that your perception of what is "twice as loud" will be affected by the kind of audio you are playing as well as the quality of the speaker and how hard it is being pushed...请注意,您对“两倍响”的感知将受到您正在播放的音频类型以及扬声器的质量以及推动它的力度的影响......

divide by 100除以 100

int intValue = 1;
float floatValue = intValue/100.0;

To map linearly to the range 0.0 to 1.0 use到 map 线性到0.01.0的范围使用

int n = <some value>;
float val = (float)(n - 1)/99;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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