2011-08-08 161 views
2

我正在學習如何在android中編寫視頻攝像頭,並編寫了一個非常基本的程序(帶有SurfaceView和兩個按鈕的XML,一個用於啓動,一個用於停止視頻錄製)。視頻預覽工作正常,但點擊start_video按鈕後,我在管線71 mediaRecorder.setVideoFrameRate(videoFramesPerSecond);得到一個IllegalStateException:IllegalStateException異常與Android MediaRecorder

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

回答

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()之前。嘗試設置視頻源,看看是否可以解決您的問題。

+0

謝謝,問題解決了! – Daniel

相關問題