我是Java新手,我的英语也不是最好的。 我想写一个应用程序,它可以确保记录的幅度。该应用程序工作除了事实,振幅始终为0.我现在已阅读几个网站,但没有找到答案。如果有人帮助我,我真的很高兴。
package com.example.volumeswitcher;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnInfoListener {
WindowManager.LayoutParams lp;
Button messe;
MediaRecorder micro;
int ampli;
TextView tv1;
TextView tv2;
String amplitext;
Vibrator vibr;
Environment e;
String mFileName;
int i;
Boolean recording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_main);
micro = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onBtnClicked(View v) {
startRecording();
}
protected void startRecording() {
if (micro == null){
i ++;
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecord"+ i +".3gp";
micro = new MediaRecorder();
micro.setAudioSource(MediaRecorder.AudioSource.MIC);
micro.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
micro.setOutputFile(mFileName);
micro.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
micro.setMaxDuration(2000);
try {
micro.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();
}
micro.start();
micro.getMaxAmplitude();
ampli = micro.getMaxAmplitude();
tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(Integer.toString(ampli));
}
micro.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
micro.stop();
recording = false;
micro.release();
micro = null;
}
}
});
}
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
}
}
感谢您的帮助!
答案 0 :(得分:0)
我有同样的问题,奇怪的是如果你改变你试图获得最大振幅的时间间隔,大于一秒,它会正常工作,但我不知道是什么引起它......