当前位置 : 主页 > 手机开发 > android >

android – 由于recorder.setAudioSource(MediaRecorder.AudioSource.MIC)导致的Forceclose错误;

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在创建一个录音机应用程序,我一整天都在谷歌搜索,我发现了一些可用于录制音频的代码.但每次我运行代码时,我的应用程序都会发出强制关闭错误并关闭. 我发现的代码如下 Media
我正在创建一个录音机应用程序,我一整天都在谷歌搜索,我发现了一些可用于录制音频的代码.但每次我运行代码时,我的应用程序都会发出强制关闭错误并关闭.
我发现的代码如下

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();
    }

我正在使用Android API 2.1

您可以找到最好的例子: http://developer.android.com/guide/topics/media/index.html#capture

我希望你在手机上测试它,因为:

Note that the emulator doesn’t have hardware to capture audio or video, but actual mobile devices are likely to provide these capabilities, accessible through the MediaRecorder class.

看看这个:

/*
 * 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" />
 */
网友评论