Android MediaRecorder在设置配置文件后崩溃

Android MediaRecorder在设置配置文件后崩溃,android,android-mediarecorder,Android,Android Mediarecorder,我在android上使用MediaRecorder将视频保存到SD卡时遇到问题 我的代码可以运行五次,直到我达到将配置文件设置为高质量的行 这是我的代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

我在android上使用MediaRecorder将视频保存到SD卡时遇到问题

我的代码可以运行五次,直到我达到将配置文件设置为高质量的行

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_sample_testing);
        // Show the Up button in the action bar.
        //setupActionBar();
        overridePendingTransition(0, 0);

        SurfaceView cameraView = (SurfaceView)findViewById(R.id.videoView1);

        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        intentTestResults = new Intent(this, TestResultsAct.class);

        TestPB = (ProgressBar) findViewById(R.id.ProgressBarTest);

        new Thread(new Runnable() {

            @Override
            public void run() {
                while(mProgressStatus < 100){
                    if(mProgressStatus == 1)
                        record();
                    if(mProgressStatus == 35)
                        record();
                    mProgressStatus += 1;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            TestPB.setProgress(mProgressStatus);
                        }
                    });
                }
            }
        }).start();
        //
    }
我的问题出现在这一行之后:recorder.setProfile(cpHigh)

这是logcat:

12-26 10:37:43.670: E/MediaRecorder(6046): try to set the audio encoder without setting the audio source first
    12-26 10:38:35.150: W/dalvikvm(6046): threadid=11: thread exiting with uncaught exception (group=0x40fee2a0)
    12-26 10:38:37.065: E/AndroidRuntime(6046): FATAL EXCEPTION: Thread-794
    12-26 10:38:37.065: E/AndroidRuntime(6046): java.lang.IllegalStateException
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at android.media.MediaRecorder.setAudioEncoder(Native Method)
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at android.media.MediaRecorder.setProfile(MediaRecorder.java:370)
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at com.example.homedevice.SampleTestingAct.initRecorder(SampleTestingAct.java:124)
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at com.example.homedevice.SampleTestingAct.record(SampleTestingAct.java:155)
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at com.example.homedevice.SampleTestingAct$2.run(SampleTestingAct.java:90)
    12-26 10:38:37.065: E/AndroidRuntime(6046):     at java.lang.Thread.run(Thread.java:856)
    12-26 10:38:47.555: E/MediaRecorder(6046): stop called in an invalid state: 4
编写的例外情况是: java.lang.IllegalStateException

我做错了什么

EDIT1(具有声音权限):

private void initRecorder() {
    camera.unlock();

    recorder.setCamera(camera);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    //recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    //recorder.setVideoSize(1920, 1080);
    //recorder.setVideoFrameRate(30);
    CamcorderProfile cpHigh = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/DCIM/Frames/videocapture_example.mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(300000000); // Approximately 5 megabytes
}
Edit2(logcat): 这是logcat

    12-26 11:32:43.170: E/AndroidRuntime(18552): FATAL EXCEPTION: Thread-1016
12-26 11:32:43.170: E/AndroidRuntime(18552): java.lang.IllegalStateException
12-26 11:32:43.170: E/AndroidRuntime(18552):    at android.media.MediaRecorder.setOutputFormat(Native Method)
12-26 11:32:43.170: E/AndroidRuntime(18552):    at android.media.MediaRecorder.setProfile(MediaRecorder.java:357)
12-26 11:32:43.170: E/AndroidRuntime(18552):    at com.example.homedevice.SampleTestingAct.initRecorder(SampleTestingAct.java:127)
12-26 11:32:43.170: E/AndroidRuntime(18552):    at com.example.homedevice.SampleTestingAct.record(SampleTestingAct.java:158)
12-26 11:32:43.170: E/AndroidRuntime(18552):    at com.example.homedevice.SampleTestingAct$2.run(SampleTestingAct.java:90)
12-26 11:32:43.170: E/AndroidRuntime(18552):    at java.lang.Thread.run(Thread.java:856)
setOutputFormat called in an invalid state: 4
threadid=11: thread exiting with uncaught exception (group=0x40fee2a0).
setProfile()函数

它总是调用setAudioEncoder(),因此如果不设置音频源 它会崩溃的

尝试在代码中包含以下内容:

recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
说明:

CamcorderProfile
还设置音频属性(如编解码器、比特率、采样器等),因此在调用之前需要设置音频源,这就是应用程序崩溃的原因。如果不想录制音频,请尝试下一个代码:

recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(WIDTH, HEIGHT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(PATH);
recorder.setPreviewDisplay(SURFACE);

recorder.prepare();
recorder.start();

是的,
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QU‌​音频编码器
已经为您设置了
音频编码器
视频编码器


卸下这些编码器,之后就可以了。

您可以检查麦克风是否未被其他应用程序使用。现在它位于
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT)中行。示例是:
java.lang.RuntimeException:setAudioSource失败。
EDIT:我也尝试在没有声音的情况下工作,但它落在起始行
recorder.start(),此选项为:
java.lang.RuntimeException:start失败。
您也添加了权限否,我不使用它,所有应用程序都已关闭。添加权限后,它再次落在设置的配置文件行中。这是错误
java.lang.IllegalStateException
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(WIDTH, HEIGHT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(PATH);
recorder.setPreviewDisplay(SURFACE);

recorder.prepare();
recorder.start();