使用Media Recorder的Android视频录制无效

时间:2015-10-09 09:03:15

标签: android camera android-mediarecorder android-videoview video-recording

我正在尝试录制视频,但它在媒体记录开始和媒体记录准备时崩溃。请帮助我...这是我的代码...

$scope.change = function(value) {
    if (value> maxValue){// Do something here}
  };

它给予了像

private boolean startRecording() {
        camera.unlock();
        try {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
            @Override
            public void onError(MediaRecorder mr, int what, int extra) {
                Log.i(TAG, "Error");
            }
        });
            second=0;
            minute=0;
            recordCountTimer = new CountDownTimer(Long.MAX_VALUE,1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    second++;
                    if(second>=60){
                        second=0;
                        minute++;
                    }
                    recordCount.setText(String.format("%02d:%02d",minute,second));
                }

                @Override
                public void onFinish() {
                    finish();
                }
            }.start();
        mediaRecorder.setCamera(camera);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            Log.d(TAG, "A");
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
            Log.e(TAG, "B");
        mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        defaultVideoPath= FileManger.getOutputMediaFile(MEDIA_TYPE_VIDEO).getAbsolutePath();
//        uriVid = Uri.parse(FileManger.getOutputMediaFile(MEDIA_TYPE_VIDEO).getAbsolutePath());
//        defaultVideoPath = getRealPathFromUri(uriVid);
        mediaRecorder.setOutputFile(defaultVideoPath);
        mediaRecorder.setVideoSize(recordingCameraSurface.getWidth(), recordingCameraSurface.getHeight());
        mediaRecorder.setVideoFrameRate(20);
            Log.v(TAG, "C");
        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
        mediaRecorder.setMaxFileSize(50000);
        mediaRecorder.prepare();
            Log.w(TAG, "D");
        mediaRecorder.start();
            Log.e(TAG, "E");
        } catch (IOException e) {
            releaseMediaRecorder();
            return false;
        }catch (IllegalStateException t){
            releaseMediaRecorder();
            return  false;
        }

        return true;
    }

在这里,我将进入下一个活动:

RECORDER_OK﹕ B
MediaRecorder﹕ setOutputFormat called in an invalid state: 4

在下一个Activity中我得到的路径为null,如:

Intent intent  = new Intent(RecordBuyPage.this,CheckAndSaveActivity.class);
            intent.putExtra("VIDEOFILEPATH", defaultVideoPath);
            startActivity(intent);

我认为我的调用媒体录音机顺序正确但它也遇到了麻烦:

 player.setDataSource(getIntent().getStringExtra("VIDEOFILEPATH"));

请给出一些有效的解决方案,我尝试了很多来自堆栈溢出,但它没有工作......我认为视频不记录,因为当我通过意图传递它时它取消了...

2 个答案:

答案 0 :(得分:0)

我希望您按照示例代码的这个链接(Media Recorderhttps://github.com/googlesamples/android-MediaRecorder

并且在record media模式下portrait中存在一些错误,因此要解决此问题,请遵循此link

在此链接中,您将media path放在stored的位置,您可以轻松地pass到另一个activity。看看我希望它可以帮到你。

要在media stop上获取capturing的路径,您可以在CaptureClick方法上执行此操作

Log.d("Video file path", CameraHelper.getOutputMediaFile(
                        CameraHelper.MEDIA_TYPE_VIDEO).toString());

并完成按钮OnClickListener

public void onCaptureClick(View view) {
        if (isRecording) {
            // BEGIN_INCLUDE(stop_release_media_recorder)

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

            // inform the user that recording has stopped
            setCaptureButtonText("Capture");
            isRecording = false;
            releaseCamera();
            Log.d("Video file path", CameraHelper.getOutputMediaFile(
                    CameraHelper.MEDIA_TYPE_VIDEO).toString());
            // END_INCLUDE(stop_release_media_recorder)

        } else {

            // BEGIN_INCLUDE(prepare_start_media_recorder)

            new MediaPrepareTask().execute(null, null, null);

            // END_INCLUDE(prepare_start_media_recorder)

        }
    }

请按照上面提供的这两个链接来解决您的问题。

答案 1 :(得分:0)

试试这个

public class VideoCapture extends Activity implements OnClickListener 
,SurfaceHolder.Callback {

MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;

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

    recorder = new MediaRecorder();
    initRecorder();
    setContentView(R.layout.main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    cameraView.setClickable(true);
    cameraView.setOnClickListener(this);
}

private void initRecorder() {
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    CamcorderProfile cpHigh = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setOutputFile("/sdcard/videocapture_example.mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

public void onClick(View v) {
    if (recording) {
        recorder.stop();
        recording = false;

        // Let's initRecorder so we can record again
        initRecorder();
        prepareRecorder();
    } else {
        recording = true;
        recorder.start();
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    prepareRecorder();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();
}
}


 Include Permissions in Manifest file :

 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />