0

目前我的代码正在检测语音强度(使用媒体记录器)我想在没有语音时将背景颜色更改为白色,当用户说话时,背景颜色必须根据语音强度变亮或变暗

这是我的代码,我在根据语音强度使颜色变亮和变暗时遇到问题。

final Runnable updater = new Runnable(){

    public void run(){          
        updateTv();
        TextView tv = (TextView)findViewById(R.id.status);
        int tvStatus=  Integer.parseInt(tv.getText().toString());
        if(tvStatus > 1000)
            updateBackground();
        else
            mScreen.setBackgroundColor(Color.WHITE);
    };
};

final Handler mHandler = new Handler();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mStatusView = (TextView) findViewById(R.id.status);
    mScreen = (LinearLayout) findViewById(R.id.myScreen);

    if (runner ==  null)
    { 
        runner = new Thread(){
            public void run()
            {
                while (runner != null)
                {


                    try
                    { 
                       Thread.sleep(1000);
                        Log.i("Noise", "Tock");
                    } catch (InterruptedException e) { };
                    mHandler.post(updater);
                }
            }
        };
        runner.start();
        Log.d("Noise", "start runner()");
    }
}

private void updateBackground()
{

     int ampl  =(int)getAmplitude();
    int color;
    Random rnd = new Random(); 

 mScreen.setBackgroundColor(

         color );

}
public void onResume()
{
    super.onResume();
    startRecorder();
}

public void onPause()
{
    super.onPause();
    stopRecorder();
}

public void startRecorder(){
    if (mRecorder == null)
    {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile("/dev/null"); 
        try
        {           
            mRecorder.prepare();
        }catch (java.io.IOException ioe) {
           android.util.Log.e("[Monkey]", "IOException: " + android.util.Log.getStackTraceString(ioe));

        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
        }
        try
        {           
            mRecorder.start();
        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
        }

        //mEMA = 0.0;
    }

}
public void stopRecorder() {
    if (mRecorder != null) {
        mRecorder.stop();       
        mRecorder.release();
        mRecorder = null;


    }
}

public void updateTv(){
    mStatusView.setText(Integer.toString((getAmplitude())));
}
public double soundDb(double ampl){
    return  20 * Math.log10(getAmplitudeEMA() / ampl);
}
public int getAmplitude() {
    if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude());
    else
        return 0;

}
4

1 回答 1

1

您当前的updateBackground()使用实现color没有初始化它:

private void updateBackground() {
    int ampl = (int) getAmplitude();
    int color;
    Random rnd = new Random(); 

    mScreen.setBackgroundColor(color);
}

如果最小幅度是0最大幅度是MAX_AMPLITUDE,并且如果您希望白色代表最小幅度,而黑色代表最大幅度,那么这样的事情应该可以解决问题:

private static final int MAX_RGB = 255;
private static final int MAX_AMPLITUDE = 32767;

private void updateBackground() {
    float amplF = (float) getAmplitude();
    int ampl = MAX_RGB - (int) (amplF / MAX_AMPLITUDE * MAX_RGB);

    mScreen.setBackgroundColor(Color.rgb(ampl, ampl, ampl));
}

如果您发现您在实践中看到的最高幅度值明显低于32767,您可以通过以下方式对此进行解释:

private static final int MAX_RGB = 255;
private static final int int MAX_AMPLITUDE = 1500; // Set to some reasonable value

private void updateBackground() {
    int actual = getAmplitude();

    if (actual > MAX_AMPLITUDE)
        actual = MAX_AMPLITUDE;

    float amplF = (float) actual;
    int ampl = MAX_RGB - (int) (amplF / MAX_AMPLITUDE * MAX_RGB);

    mScreen.setBackgroundColor(Color.rgb(ampl, ampl, ampl));
}

如果你这样做,最好MAX_AMPLITUDE不要再设置一个常数,并通过提供“校准”选项使其可配置,用户可以在其中制作他们认为是响亮的任何东西。

于 2012-07-06T05:19:34.960 回答