Android中的录像机应用程序

时间:2014-08-06 01:25:26

标签: android video-capture

我是Android和Java的新手,请原谅我,如果答案很明显,但我不能让我的录像机应用程序工作。我可以在相机预览中看到图像很好(该代码工作正常)但是当我去录制时,我只录制声音。我已经实现了Google推荐的代码片段(如下所示),但它无效。我已经尝试了所有我能想到的东西。任何见解都将不胜感激。

protected void onCreate(Bundle savedInstanceState) {
    int x;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_camera);

    // if (savedInstanceState == null) {
    //  getSupportFragmentManager().beginTransaction()
    //          .add(R.id.container, new PlaceholderFragment()).commit();
    // Create an instance of Camera
    x =  Camera.getNumberOfCameras();
    mCamera = getCameraInstance(x-1);
    recorder = new MediaRecorder();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new VideoCameraPreview(this, x-1, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.video_camera_preview);
    preview.addView(mPreview);

    try {
        mCamera.setPreviewDisplay(VideoCameraPreview.mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Add a listener to the Capture button
    final Button captureButton = (Button) findViewById(R.id.button_capture);

    captureButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isRecording) {
                    // stop recording and release camera
                    recorder.stop();  // stop the recording
                    // releaseMediaRecorder(); // release the MediaRecorder object
                    mCamera.lock();         // take camera access back from MediaRecorder

                    // inform the user that recording has stopped
                    captureButton.setText("Capture");
                    isRecording = false;
                } else {
                    // initialize video camera
                    if (prepareVideoRecorder()) {
                        // Camera is available and unlocked, MediaRecorder is prepared,
                        // now you can start recording
                        recorder.start();

                        // inform the user that recording has started
                        captureButton.setText("Stop");
                        isRecording = true;
                    } else {
                        // prepare didn't work, release the camera
                        releaseMediaRecorder();
                        // inform user
                    }
                }
            }
        }
    );

}

private boolean prepareVideoRecorder (){
    //Record a video in response to a user pressing the button

    int x = Camera.getNumberOfCameras();

    mCamera.unlock();  //Unlock the camera for use by the Media Recorder
    recorder.setCamera(mCamera); // Get the camera ready for recording
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    recorder.setProfile(CamcorderProfile.get(x-1, CamcorderProfile.QUALITY_HIGH));

    recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
    recorder.setPreviewDisplay(VideoCameraPreview.mHolder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("Wink", "Illegal stateException preparing MediaRecorder: " + e.getMessage());
        recorder.release();
        return false;
    } catch (IOException e) {
        Log.d("Wink", "IOException preparing MediaRecorder: " + e.getMessage());
        recorder.release();
        return false;
    }

    return true;
}       

2 个答案:

答案 0 :(得分:0)

我还没有深入讨论过这个问题,但是这里有一个链接到另一个可能对你有帮助的Android录像机。

Display timer while video recording android

答案 1 :(得分:0)

更改setVideoSource和setAudioSource并尝试设置视频的输出格式并设置两个编码器。

recorder.setCamera(mCamera); // Get the camera ready for recording
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

recorder.setProfile(CamcorderProfile.get(x-1, CamcorderProfile.QUALITY_HIGH));

recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
recorder.setPreviewDisplay(VideoCameraPreview.mHolder.getSurface());