我开发了这个小型的MediaRecorder应用程序。我有3个按钮 - Play
(用于开始录制),Pause
(用于停止录制),Max
(用于查找最大振幅)。我用谷歌搜索得到它是正确的,但没有什么工作对我好。
问题是getMaxAmplitude
总是给我0。
以下是我正在使用的代码。任何人都可以帮忙
public class MainActivity extends Activity {
Button play,pause, max;
String outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myexampl.3gp";
MediaRecorder mrecorder=new MediaRecorder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button)findViewById(R.id.button1);
pause= (Button)findViewById(R.id.button2);
max = (Button)findViewById(R.id.button3);
mrecorder = new MediaRecorder();
mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mrecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mrecorder.setOutputFile(outputFile);
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mrecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mrecorder.start();
Toast.makeText(getApplicationContext(), "Starts Recording!",
Toast.LENGTH_SHORT).show();
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mrecorder.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mrecorder.release();
Toast.makeText(getApplicationContext(), "Stops Recording!",
Toast.LENGTH_SHORT).show();
}
});
max.setOnClickListener(new OnClickListener() {
int ampl=0;
@Override
public void onClick(View v) {
try {
ampl=mrecorder.getMaxAmplitude();
} catch (IllegalStateException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), String.valueOf(ampl),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}