我想使用媒体记录器

时间:2015-10-09 07:04:45

标签: android video camera mediarecorder

我想使用Surface视图摄像头录制视频和Media Recorder这是我的代码....我在mediarecorder.prepare()崩溃,我的默认路径变为null。请帮帮我......

private boolean startRecording() {
        try {
        camera.unlock();
        mediaRecorder = new MediaRecorder();
            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.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            Log.d(TAG, "A");
//        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);
            CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            mediaRecorder.setProfile(camcorderProfile);
        mediaRecorder.setOutputFile(defaultVideoPath);
        mediaRecorder.setVideoSize(recordingCameraSurface.getWidth(), recordingCameraSurface.getHeight());
        mediaRecorder.setVideoFrameRate(20);
            Log.v(TAG, "C");
        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
        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;
    }

当我在意图中发送此默认视频路径时,路径将变为空

1 个答案:

答案 0 :(得分:0)

您好请尝试以下函数获取您获得null的videoPath

public Uri getOutputMediaFileUri(int p_type)
    {
        return Uri.fromFile(getOutputMediaFile(p_type));
    }


    private File getOutputMediaFile(int p_type)
    {
        File m_mediaFile;
        if (p_type == MEDIA_TYPE_VIDEO)
        {   
            // External sdcard location
            File m_videoStorageFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Videos");
            // Create the storage directory if it does not exist
            if (!m_videoStorageFile.exists())
            {
                if (!m_videoStorageFile.mkdirs())
                {
                    return null;
                }
            }
            // Create a media file name
            String m_timeStamp = new SimpleDateFormat(Constant.TIME_STAMP_FORMAT, Locale.getDefault()).format(new java.util.Date());
            m_buffer = new StringBuffer();
            m_buffer.append(m_videoStorageFile.getPath()).append(File.separator).append("VID_").append(m_timeStamp).append(".mp4");
            m_mediaFile = new File(m_buffer.toString());
        }
        else
        {
            return null;
        }

        return m_mediaFile;
    }

使用功能

int MEDIA_TYPE_VIDEO = 0;
private File defaultVideoPath;
String videoPath;

fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
videoPath = fileUri.getPath();

将videoPath从一个活动传递到另一个活动

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

在CheckAndSaveActivity.class中写下以下代码

if(getIntent().getExtras() != null)
{
      String imagePath = getIntent().getExtras().getString("VIDEOFILEPATH");
}

希望您已在Manifiest中声明了CheckAndSaveActivity