【问题标题】:I want to record a video in android using media recorder我想使用媒体记录器在 android 中录制视频
【发布时间】:2015-10-09 07:04:44
【问题描述】:

我想使用 Surface 视图摄像头和媒体记录器录制视频这是我的代码....我在 mediarecorder.prepare() 处崩溃并且我的默认路径为空。请帮帮我...

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

当我有意发送此默认视频路径时,路径变为空

【问题讨论】:

    标签: android video camera mediarecorder


    【解决方案1】:

    你好,请尝试以下函数来获取你得到 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");
    }
    

    希望您已在清单中声明 CheckAndSaveActivity

    【讨论】:

    • 对不起,我有默认路径但是当 Mediarecorder 启动时它失败了。但是当我发送它时,在下一个活动中它得到 Null
    • 是的。 Intent(RecordBuyPage.this,CheckAndSaveActivity.class); intent.putExtra("VIDEOFILEPATH", defaultVideoPath);开始活动(意图);在下一个活动中,它在... player.setDataSource(getIntent().getStringExtra("VIDEOFILEPATH"));
    • 不,它不工作,我认为视频不是正在录制,因为它会变得空。
    • 我很困惑您所面临的实际问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多