[英]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.