[英]sound recording problem in android
我正在为平板电脑创建一个录音机应用程序。我尝试了下面的代码来录制声音。但是当我单击停止按钮时它崩溃了...
public class soundrecord extends Activity {
private Button start;
private Button stop;
private TextView txt;
MediaRecorder recorder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
txt=(TextView)findViewById(R.id.txtstatus);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/myfile/temp");
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
txt.setText("recording");
recorder.prepare();
recorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}) ;
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txt.setText("stop recording");
recorder.stop();
/*recorder.reset();
recorder.release();*/
}
}) ;
}
}
这是logcat中显示的异常
05-12 15:49:01.013:错误/ AndroidRuntime(677):java.lang.IllegalStateException
如果在start()之前调用,则抛出IllegalStateException
现在,我假设您没有在开始之前调用它。 我想知道您的onCreate()是否正确进行。 如果由于某种原因第二次调用onCreate()(例如,屏幕旋转),则会创建一个新的MediaRecorder,并且您将在尚未开始调用的记录器上调用stop()。
另外,您确定start()正常工作吗? IllegalStateException可能来自start()。 您能给我们完整的堆栈跟踪信息来说明IllegalStateException的来源吗?
继续执行您给出的例外。 这可能是因为听力计目录不存在。 如果使用File.mkdir()创建此文件,则它将正常工作。 或者,尝试使用示例中给出的顶级文件名运行: http : //developer.android.com/guide/topics/media/index.html
recorder.setOutputFile("/audiorecordtest.3gp");
请通过以下链接。 这是我在developer.android.com中找到的示例代码,可能会对您有所帮助http://developer.android.com/guide/topics/media/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.