正在制作一个应用程序,我想在仪表上显示麦克风输入的声音强度:
这是我的一个班级:
public class Kissometer extends Activity {
MediaRecorder mRecorder;
Thread runner = null;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;
double hello = 0;
TextView Result;
final Runnable updater = new Runnable(){
public void run(){
updateTv();
};
};
final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kissometer);
Button Launch = (Button)findViewById(R.id.button1);
Button Stop = (Button) findViewById(R.id.button2);
Result = (TextView) findViewById(R.id.textView1);
if (runner == null)
{
runner = new Thread(){
public void run()
{
while (runner != null)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Noise", "Tock");;
mHandler.post(updater);
}
}
};
runner.start();
Log.d("Noise", "start runner()");
}
Launch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startRecorder();
hello= hello+6000;
hello = hello/100;
Intent bhejo = new Intent(Kissometer.this, GuageWork.class);
Bundle b = new Bundle();
b.putDouble("Needle_Value", hello);
bhejo.putExtras(b);
startActivity(bhejo);
}
});
Stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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();
mRecorder.setMaxDuration(1000);
}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(){
Result.setText(Double.toString(getAmplitudeEMA()));
}
public double soundDb(double ampl){
return 20 * Math.log10(getAmplitudeEMA() / ampl);
}
public double getAmplitude() {
if (mRecorder != null)
return (mRecorder.getMaxAmplitude());
else
return 0;
}
public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kissometer, menu);
return true;
}
}
这是另一个类:
public class GuageWork extends Activity {
private GaugeView mGaugeView1;
private GaugeView mGaugeView2;
double lol;
private final Random RAND = new Random();
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
lol = b.getDouble("Needle_Value");
setContentView(R.layout.guage_kaam);
mGaugeView1 = (GaugeView) findViewById(R.id.gauge_view1);
mTimer.start();
}
private final CountDownTimer mTimer = new CountDownTimer(30000, 1000) {
@Override
public void onTick(final long millisUntilFinished) {
mGaugeView1.setTargetValue(lol);
//mGaugeView2.setTargetValue(30);
//mGaugeView2.setTargetValue(RAND.nextInt(101));
}
@Override
public void onFinish() {}
};
}
这是两个类。现在发生的事情是,第一堂课的声音记录时间为几毫秒。之后,它将值发送到下一个基本上是计量表类的活动。我正在使用图书馆作为支持项目: 第一个XML文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:gauge="http://schemas.android.com/apk/res/org.codeandmagic.android.gauge.samples"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/background"
android:padding="20dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/button1"
android:text="Stop Recording" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:layout_marginTop="48dp"
android:layout_toLeftOf="@+id/textView1"
android:text="Start Recording" />
</RelativeLayout>
以下XML涉及第二类,即第二类:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:gauge="http://schemas.android.com/apk/res/org.codeandmagic.android.gauge.samples"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/background"
android:padding="20dp" >
<org.codeandmagic.android.gauge.GaugeView
android:id="@+id/gauge_view1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
我不明白为什么我的程序在录制了几毫秒后启动其他活动时崩溃了。 logcat显示以下内容:
09-11 10:23:26.960: E/AndroidRuntime(1009): FATAL EXCEPTION: main
09-11 10:23:26.960: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lips.kiss_o_meter/com.lips.kiss_o_meter.GuageWork}: android.view.InflateException: Binary XML file line #11: Error inflating class org.codeandmagic.android.gauge.GaugeView
09-11 10:23:26.960: E/AndroidRuntime(1009): Caused by: java.lang.reflect.InvocationTargetException
09-11 10:23:26.960: E/AndroidRuntime(1009): at java.lang.reflect.Constructor.constructNative(Native Method)
09-11 10:23:26.960: E/AndroidRuntime(1009): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-11 10:23:26.960: E/AndroidRuntime(1009): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-11 10:23:26.960: E/AndroidRuntime(1009): ... 21 more
09-11 10:23:26.960: E/AndroidRuntime(1009): Caused by: java.lang.NoClassDefFoundError: org.codeandmagic.android.gauge.R$styleable
09-11 10:23:26.960: E/AndroidRuntime(1009): at org.codeandmagic.android.gauge.GaugeView.readAttrs(GaugeView.java:183)
09-11 10:23:26.960: E/AndroidRuntime(1009): at org.codeandmagic.android.gauge.GaugeView.<init>(GaugeView.java:170)
09-11 10:23:26.960: E/AndroidRuntime(1009): at org.codeandmagic.android.gauge.GaugeView.<init>(GaugeView.java:175)