在MediaStore.Video.Media.DURATION期限错 [英] Wrong DURATION in MediaStore.Video.Media.DURATION
本文介绍了在MediaStore.Video.Media.DURATION期限错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的应用程序来记录的视频,我已经注意到,展示他们的持续时间后,我看错分钟\\秒。出现这种情况只与视频录制槽下面code。随着视频通过其他应用程序记录,持续时间显示正确的:
公共无效recordStream(){ // MediaRecorder开始前释放相机
releaseCamera(); 如果(!prepareMediaRecorder()){
Toast.makeText(MainActivity.this,
在失败prepareMediaRecorder()\\ n - 端 -
Toast.LENGTH_LONG).show(); } mediaRecorder.start();
}
} 私人布尔prepareMediaRecorder(){ myCamera = getCameraInstance();
mediaRecorder =新MediaRecorder();
myCamera.stop preVIEW();
myCamera.unlock();
mediaRecorder.setCamera(myCamera); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); CamcorderProfile轮廓= CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth,profile.videoFrameHeight);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mediaRecorder.setAudioChannels(profile.audioChannels);
mediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mediaRecorder.setVideoEn codeR(profile.video codeC);
mediaRecorder.setAudioEn codeR(profile.audio codeC);
}
//字符串outputfilename;
日期日期=新的日期();
。outputfilename =/ SD卡/视频/+与Date.toString()代替(,_)。替换(:,_)+MP4 mediaRecorder.setOutputFile(outputfilename);
mediaRecorder.set previewDisplay(mSurfaceView.getHolder()getSurface()); 尝试{
mediaRecorder prepare()。
}赶上(IllegalStateException异常五){
releaseMediaRecorder();
返回false;
}赶上(IOException异常五){
releaseMediaRecorder();
返回false;
} 返回true; }
这是我如何从视频持续时间:
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
videocursor.moveToPosition(位置); 持续时间长= videocursor.getLong(video_column_index); 字符串转换=的String.format(%02D:%02D
TimeUnit.MILLISECONDS.toMinutes(持续时间),
TimeUnit.MILLISECONDS.toSeconds(持续时间) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(持续时间))
); holder.txtDuration.setText(转换);
解决方案
在这样解决的:
MediaPlayer的MP = MediaPlayer.create(这一点,Uri.parse(outputfilename));
INT持续时间= mp.getDuration();
mp.release(); ContentValues值=新ContentValues();
values.put(MediaStore.Video.Media.DATA,outputfilename);
values.put(MediaStore.Video.Media.DATE_TAKEN,dateTaken);
values.put(MediaStore.Video.Media.DURATION,持续时间);
这是计算和视频持续时间添加到MediaStore性能的需求。
I'm trying to record video in my application and I've noticed that displaying their duration, I see wrong minutes \ seconds. This happens only with the video recorded trough the following code. With the video recorded through other apps, the duration is displayed right:
public void recordStream() {
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(MainActivity.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
}
mediaRecorder.start();
}
}
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.stopPreview();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mediaRecorder.setAudioChannels(profile.audioChannels);
mediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setAudioEncoder(profile.audioCodec);
}
//String outputfilename;
Date date=new Date();
outputfilename="/sdcard/video/"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
mediaRecorder.setOutputFile(outputfilename);
mediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
This is how I get duration from the video:
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
videocursor.moveToPosition(position);
long duration = videocursor.getLong(video_column_index);
String converted = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);
holder.txtDuration.setText(converted);
解决方案
solved in this way:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(outputfilename));
int duration = mp.getDuration();
mp.release();
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, outputfilename);
values.put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
values.put(MediaStore.Video.Media.DURATION, duration);
It was the need to calculate and add the video duration to the MediaStore properties.
这篇关于在MediaStore.Video.Media.DURATION期限错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文