0

我不断收到“11-15 01:00:53.185: E/MediaRecorder(976): setOutputFormat called in an invalid state: 1” on "recorder.setProfile(profile); "

package com.apress.proandroidmedia.ch1.cameraintent;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class JustRecord extends Activity implements SurfaceHolder.Callback{

    SurfaceView cameraView;
    SurfaceHolder surfaceHolder;
    Camera camera;
    MediaRecorder recorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        cameraView = (SurfaceView) findViewById(R.id.CameraView);
        surfaceHolder = cameraView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(this);

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        try{
            camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
            camera.setDisplayOrientation(90);

        }
        catch (Exception exception){
            camera.release();
            exception.printStackTrace();
        }


        recorder = new MediaRecorder();
        recorder.setCamera(camera);

        CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_HIGH);
        if(profile != null) {
            recorder.setProfile(profile);  

        }else {
            //default to basic H263 and AMR_NB if profile not found
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);       
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        }


        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        recorder.setMaxDuration(5000); // 50 seconds

        prepareRecorder();
        recorder.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        //camera.stopPreview();
        recorder.stop();
        recorder.release();
        camera.release();
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

}

有任何想法吗?谢谢!

编辑:我不妨补充一点,如果我输入编解码器等,代码就可以正常工作。只是由于某种原因我无法确定最好的质量。

4

1 回答 1

2

我设法通过实际开始考虑改变来解决这个问题......

当您查看 MediaRecorder 流程​​图时,您首先看到的是定义的 AudioSource 和 VideoSource。此外,在使用 CamcorderProfile 时,显然没有定义这 2 个。然而,在定义了这两行之后,MediaRecorder 仍然拒绝启动。这里缺少的是“camera.unlock()”,因为我的相机在之前的测试中被“拍摄”了。

于 2012-11-15T20:28:55.357 回答