简体   繁体   English

我想使用媒体录制器在android中录制视频

[英]I want to record a video in android using media recorder

I want record a video using Surface view camera and Media Recorder Here is My Code.... I am getting crash at mediarecorder.prepare() and my default path getting null. 我想使用Surface View摄像机和Media Recorder录制视频,这是我的代码...。我在mediarecorder.prepare()崩溃,并且我的默认路径为空。 please help me ... 请帮我 ...

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

When I am sending this default video path in intent the path is getting null 当我故意发送此默认视频路径时,该路径将为空

hi please try below functions for getting videoPath which you got null 您好,请尝试使用以下函数获取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;
    }

Use of function 使用功能

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

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

Pass videoPath from one activity to another 将videoPath从一项活动传递到另一项活动

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

write below code in your CheckAndSaveActivity.class 在CheckAndSaveActivity.class中编写以下代码

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

Hope you have declared CheckAndSaveActivity in your Manifiest 希望您在Manifiedt中声明了CheckAndSaveActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Media Recorder的Android视频录制无法正常工作 - Android Video Recording using Media Recorder is Not working 需要一个代码,用于在Android 2.1中使用媒体记录器录制高质量的视频? - Need a code for record a video with high quality in android 2.1 using Media recorder? 不使用android媒体记录器的Android视频录制 - Android Video Recording without using android media recorder 使用SurfaceView和媒体录制器的Android中的视频录制器应用程序:录制视频时,它向左显示90度弯曲 - Video recorder app in Android using SurfaceView and media recorder: while recording video, it shows the 90 degree bend to left Android:使用默认的录像机录制并返回SD卡路径 - Android: Record using default video recorder and return the sd card path Android Media Recorder-片段录制音频 - Android Media Recorder - Record Audio in Pieces 在 android 中使用媒体记录器获取视频的经过时间 - getting elapsed time of video with media recorder in android 如何使用自定义媒体记录器进行记录? - How to record using custom media recorder? 使用媒体记录器进行视频录制 - Video recording with media recorder 在Android的下一个活动的videoView中传输记录的摄像机视频(使用Media Recorder) - Transfer recorded camera video (using Media Recorder) in videoView of next activity in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM