【问题标题】:decreasing and increasing float increment android减少和增加浮动增量android
【发布时间】:2016-03-07 15:31:28
【问题描述】:

我正在尝试通过屏幕上的按钮来增加和减少音量。我这里有代码:

//variables
float leftVolume = 0.5f;
float rightVolume = 0.5f;

public void lowerVolume(View view)
{ float decrement = 0.2f;
    rightVolume -= decrement;
    leftVolume -= decrement;
    Log.d("Values","This is the value of volume"+ leftVolume);
}

public void raiseVolume(View view)
{ float increment = 0.2f;
    rightVolume = ++increment;
    leftVolume = ++increment;
    Log.d("Values","This is the value of volume"+ rightVolume);
}

日志显示了一些疯狂的值,例如当我单击 rasieVolume 时,它​​会转到 1.2f 并停留在那里。

【问题讨论】:

    标签: java android audio increment


    【解决方案1】:

    发生这种情况是因为您每次调用 raiseVolume() 时都将浮点值设置为 1.2。

    public void raiseVolume(View view)
    { float increment = 0.2f; //you set increment here
        rightVolume = ++increment; //then simply increase it by 1
        leftVolume = ++increment;
    
        //...and so your volume will always be 1.2 at this point
        Log.d("Values","This is the value of volume"+ rightVolume);
    }
    

    解决此问题的方法是将音量设置为raiseVolume() 方法之外的初始值(您已经这样做了),然后在raiseVolume() 方法内部增加它。

    像这样:

    //variables
    float leftVolume = 0.5f;
    float rightVolume = 0.5f;
    
    public void raiseVolume(View view)
    { float increment = 0.2f;
        rightVolume += increment; //change this line to this
        leftVolume += increment;  //change this line to this
        Log.d("Values","This is the value of volume"+ rightVolume);
    }
    

    【讨论】:

    • 啊,我现在明白了,谢谢。那么每次单击按钮时,我如何让它减少 0.2 呢?很抱歉,如果这很明显!
    • 从您的原始代码来看,您的 lowerVolume() 方法应该按预期工作。声明rightVolume -= decrement; 应该正确地降低您的音量级别。很高兴为您提供帮助,如果您认为它解决了您的问题,请接受我的回答!
    【解决方案2】:

    因为这段代码会把increment增加到1.2f

     rightVolume = ++increment;
    

    那么rightVolume 将是1.2f,而leftVolume 将是2.2f

    所以你应该像在lowerVolume 中那样更改值:

    rightVolume += increment;
    leftVolume += increment; 
    

    【讨论】:

      【解决方案3】:

      每次您将增量值增加 1 并将其分配到右侧或左侧音量时,请使用以下操作。

      rightVolume += increment;
      leftVolume += increment; 
      

      【讨论】:

        【解决方案4】:

        所有给出的答案在严格意义上都是正确的,但我只想指出典型的音频音量控制是对数锥形而不是线性的。这与对响度的感知有关。如果您想遵循该模型,那么您需要乘除而不是加减。另外,另一个注意事项:任何数字信号处理中使用的音频音量都希望保持在 0 到 1 的范围内,其中 1.0 表示完全输出电平,0 表示静音。如果您想要这种行为,那么您还应该在 raiseVolume 过程中将值剪辑为

        //variables
        float leftVolume = 0.5f;
        float rightVolume = 0.5f;
        
        public void lowerVolume(View view)
        { 
           float decrement = 0.5f;
           rightVolume *= decrement;
           leftVolume *= decrement;
        }
        
        public void raiseVolume(View view)
        { 
            float increment = 2.0f;
            rightVolume *= increment;
            leftVolume *= increment;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-25
          • 2010-12-01
          • 2015-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多