android录制视频,初始化摄像头

时间:2012-11-29 20:02:48

标签: android

public class FulfillVideoTaskActivity extends Activity implements SurfaceHolder.Callback, OnInfoListener, OnErrorListener{

private Button initBtn = null;
private Button startBtn = null;
private Button stopBtn = null;
private Button playBtn = null;
private Button stopPlayBtn = null;
// save Button should be implemented
private TextView recordingMsg = null;
private VideoView videoView = null;
private SurfaceHolder holder = null;
private Camera camera = null;
private static final String TAG ="RecordVideo";
private MediaRecorder recorder = null;
private String outputFileName;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fulfill_video_task);

    // get references to UI elements
    initBtn = (Button) findViewById(R.id.initBtn);
    startBtn = (Button) findViewById(R.id.startBtn);
    stopBtn = (Button) findViewById(R.id.stopBtn);
    playBtn = (Button) findViewById(R.id.playBtn);
    stopPlayBtn = (Button) findViewById(R.id.stopPlayBtn);
    recordingMsg = (TextView) findViewById(R.id.recording);
    videoView = (VideoView)this.findViewById(R.id.videoView);


}

public void buttonTapped(View view) {
    switch(view.getId()) {
    case R.id.initBtn:
        initRecorder();
        break;
    case R.id.startBtn:
        beginRecording();
        break;
    case R.id.stopBtn:
        stopRecording();
        break;
    case R.id.playBtn:
        playRecording();
        break;
    case R.id.stopPlayBtn:
        stopPlayback();
        break;
    }
}
private void stopPlayback() {
    videoView.stopPlayback();

}

private void playRecording() {
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);
    videoView.setVideoPath(outputFileName);
    videoView.start();
    stopPlayBtn.setEnabled(true);

}

private void stopRecording() {
    if(recorder != null) {
        recorder.setOnErrorListener(null);
        recorder.setOnInfoListener(null);
        try {
            recorder.stop();

        }
        catch(IllegalStateException e) {
            //this can happen if the recorder has already stopped.
            Log.e(TAG, "Got IllegalStateException in stopRecording");

        }
        releaseRecorder();
        recordingMsg.setText("");
        releaseCamera();
        startBtn.setEnabled(false);
        stopBtn.setEnabled(false);
        playBtn.setEnabled(true);

    }


}


private void releaseCamera() {
    if(camera != null) {
        try {
            camera.reconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
        camera.release();
        camera = null;

    }

}

private void releaseRecorder() {
    if(recorder != null) {
        recorder.release();
        recorder = null;
    }

}

private void beginRecording() {
    recorder.setOnInfoListener(this);
    recorder.setOnErrorListener(this);
    recorder.start();
    recordingMsg.setText("RECORDING");
    startBtn.setEnabled(false);
    stopBtn.setEnabled(true);

}

// Initialize the recorder
private void initRecorder() {
    if(recorder != null) return;

    // The place where the video will be saved.
    outputFileName = Environment.getExternalStorageDirectory() + "/videooutput.mp4";

    File outFile = new File(outputFileName);
    //if File already exists, we delete it 
    if(outFile.exists())
        outFile.delete();

    try{
        camera.stopPreview();
        camera.unlock();
        recorder = new MediaRecorder();
        recorder.setCamera(camera);

        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setVideoSize(280, 200);
        recorder.setVideoFrameRate(15);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setMaxDuration(10000); // limit to 10 seconds
        recorder.setPreviewDisplay(holder.getSurface());
        recorder.setOutputFile(outputFileName); // setting our output file to our FileName

        recorder.prepare();
        Log.v(TAG, "MediaRecorder initialized");
        initBtn.setEnabled(false);
        startBtn.setEnabled(true);

    }
    //error checking 
    catch(Exception e) {
        Log.v(TAG, "MediaRecorder failed to initialize");
        e.printStackTrace();

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_fulfill_video_task, menu);
    return true;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    Log.v(TAG, "in sufaceCreated");

    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();

    }catch (IOException e) {
        Log.v(TAG, "Could not start the preview");
        e.printStackTrace();

    }
    initBtn.setEnabled(true);

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void onError(MediaRecorder mr, int what, int extra) {
    Log.e(TAG, "got a recording error");
    stopRecording();
    Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show();

}

public void onInfo(MediaRecorder mr, int what, int extra) {
    Log.i(TAG, "got a recording event");
    if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
        Log.i(TAG, "...max duration reached");
        stopRecording();
        Toast.makeText(this, "Recording limit has been reached. Stopping the recording", Toast.LENGTH_SHORT).show();

    }


}

// disable the buttons until the camera is initialized
protected void onResume() {
    Log.v(TAG, "in onResume");
    super.onResume();
    initBtn.setEnabled(false);
    startBtn.setEnabled(false);
    stopBtn.setEnabled(false);
    playBtn.setEnabled(false);
    stopPlayBtn.setEnabled(false);
    if(!initCamera())
        finish();

}

// initializes the camera 

private boolean initCamera() {
    try {
        camera = Camera.open();
        Camera.Parameters camParams = camera.getParameters();
        camera.lock();
        holder = videoView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    catch(RuntimeException re) {
        Log.v(TAG, "Could not initialize the Camera");
        re.printStackTrace();
        return false;
    }
    return true;
}

}

嗨,我正在尝试在Android上录制视频,当我运行我的代码(上面的整个代码)时,相机无法初始化。我猜我在下面的部分中有错误。

private boolean initCamera() {
    try {
        camera = Camera.open();
        Camera.Parameters camParams = camera.getParameters();
        camera.lock();
        holder = videoView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    catch(RuntimeException re) {
        Log.v(TAG, "Could not initialize the Camera");
        re.printStackTrace();
        return false;
    }
    return true;
}

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);此代码中的这一行有多个标记,说

  • 不推荐使用SurfaceHolder类型的方法setType(int)
  • 不推荐使用字段SURFACE_TYPE_PUSH_BUFFERS

有谁知道我为什么会这样做?

1 个答案:

答案 0 :(得分:3)

试试此代码

首先

   setRecorder() ;
   SurfaceView  videoShootSurfaceView = (SurfaceView) findViewById(R.id.shootVideosurfaceView_VSD);


    SurfaceHolder videoSurfaceHolder = videoShootSurfaceView.getHolder();
    videoSurfaceHolder.addCallback(this);
    videoSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

设置录像机类型

    public void setRecorder() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    // recorder.setVideoSize(640, 480);
    recorder.setVideoSize(320, 240);
    // recorder.setVideoSize(480, 320);
    // recorder.setVideoSize(176, 144);
    recorder.setVideoFrameRate(15);
    // recorder.setMaxDuration(3600000);
    recorder.setMaxDuration(300000);
    recorder.setOutputFile("/sdcard/videocapture_example.mp4");
}

覆盖这个方法。

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    recorder.setPreviewDisplay(holder.getSurface());
    if (recorder != null) {
        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            Log.e("IllegalStateException", e.toString());
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        }
    }
}

开始录制

    public void startRecording() {
    setRecorder();
    recorder.setPreviewDisplay(recoderTempHolder.getSurface());
    if (recorder != null) {
        try {
            recorder.prepare();
            recorder.start();
        } catch (IllegalStateException e) {
            Log.e("IllegalStateException", e.toString());
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        }
    }

}

停止录制

  public void stopRecording() {

    recorder.stop();
    // recorder.reset();
    recorder.release();

}