簡體   English   中英

我想使用媒體錄制器在android中錄制視頻

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

我想使用Surface View攝像機和Media Recorder錄制視頻,這是我的代碼...。我在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;
    }

當我故意發送此默認視頻路徑時,該路徑將為空

您好,請嘗試使用以下函數獲取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");
}

希望您在Manifiedt中聲明了CheckAndSaveActivity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM