使用android MediaRecorder的IllegalStateException

时间:2011-08-08 00:31:48

标签: android camera mediarecorder

我正在学习如何在Android中对摄像机进行编程,并编写了一个非常基本的程序(带有SurfaceView和两个按钮的XML,一个启动,一个停止视频录制)。视频预览工作正常,但点击start_video按钮后,我在第71行mediaRecorder.setVideoFrameRate(videoFramesPerSecond);收到IllegalStateException:

IllegalStateException.<init>() line: 33 
MediaRecorder.setVideoFrameRate(int) line: not available [native method]    
CamtestActivity$2.onClick(View) line: 71

。我无法理解为什么这一行抛出这个异常,而前一行例如却没有。 任何提示?感谢。

package com.grapp.camtest;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class CamtestActivity extends Activity implements SurfaceHolder.Callback{
    private static final String TAG = "Camera-Tutorial";

    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Camera camera;
    private boolean previewRunning;

    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final int videoFramesPerSecond = 20;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        Button start_video = (Button) findViewById(R.id.start_video);
        Button stop_video = (Button) findViewById(R.id.stop_video);


        stop_video.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaRecorder.stop();
                camera.lock();
            }
        });

        start_video.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    camera.unlock();

                    mediaRecorder = new MediaRecorder();
                    mediaRecorder.setCamera(camera);
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    mediaRecorder.setMaxDuration(maxDurationInMs);
                    mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                    mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());
                    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
                    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface()); 
                    mediaRecorder.setOutputFile("/sdcard/video.mp4");
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IllegalStateException e) {
                    Log.e(TAG,e.getMessage());
                    e.printStackTrace();

                } catch (IOException e) {
                    Log.e(TAG,e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        if (camera != null){
            Camera.Parameters params = camera.getParameters();
            camera.setParameters(params);
        }
        else {
            Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (previewRunning){
            camera.stopPreview();
        }
        Camera.Parameters p = camera.getParameters();
        p.setPreviewSize(width, height);
        camera.setParameters(p);

        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        previewRunning = false;
        camera.release();
    }
}

1 个答案:

答案 0 :(得分:3)

来自setVideoFrameRate()的文档:

Sets the frame rate of the video to be captured.
Must be called after setVideoSource().
Call this after setOutFormat() but before prepare().

在您尝试setVideoSource()之前,您似乎尚未调用setVideoFrameRate()。尝试设置视频源,看看是否能解决您的问题。