我正在创建一个录音机应用程序,我一整天都在谷歌搜索,我发现了一些可用于录制音频的代码.但每次我运行代码时,我的应用程序都会发出强制关闭错误并关闭.我发现的代码如下
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); ***<== ERROR***
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/sample.3gp");
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Android API 2.1
小智 7
您可以在这里找到最好的例子:http://developer.android.com/guide/topics/media/index.html#capture
我希望你在手机上测试它,因为:
请注意,仿真器没有用于捕获音频或视频的硬件,但实际的移动设备可能提供这些功能,可通过MediaRecorder类访问.
看看这个:
/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5845 次 |
| 最近记录: |