开发者

IllegalStateException with android MediaRecorder

开发者 https://www.devze.com 2023-03-26 07:22 出处:网络
I\'m learning how to program the video camera in android and wrote a very basic program (XML with SurfaceView and two buttons, one to start, one to stop video recording). Video Preview works fine, but

I'm learning how to program the video camera in android and wrote a very basic program (XML with SurfaceView and two buttons, one to start, one to stop video recording). Video Preview works fine, but after clicking the start_video button I'm getting an IllegalStateException in line 71 mediaRecorder.setVideoFrameRate(videoFramesPerSecond);:

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

. I can't understand why this line is throwing this exception while the former line for example, does not. Any hints? Thanks.

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开发者_如何学JAVA_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();
    }
}


From the documentation for setVideoFrameRate():

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

It seems like you have not yet called setVideoSource() before you try to setVideoFrameRate(). Try setting the video source and see if that solves your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消