目前我的代码正在检测语音强度(使用媒体记录器)我希望在没有语音时将背景颜色更改为白色,当用户说话时,背景颜色必须根据语音强度亮或暗
这是我的代码即时根据语音强度制作颜色明暗的问题。
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;
}
答案 0 :(得分:1)
您当前的updateBackground()
实施使用color
而未初始化:
private void updateBackground() {
int ampl = (int) getAmplitude();
int color;
Random rnd = new Random();
mScreen.setBackgroundColor(color);
}
如果最小幅度为0
且最大幅度为MAX_AMPLITUDE
,并且如果您希望白色表示最小幅度和黑色,最大幅度,那么这样的事情应该可以解决问题:< / p>
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
不再是常数可能是一个好主意,并通过提供“校准”选项使其可配置,用户可以做出他们认为是大声的任何内容噪声