gpt4 book ai didi

android - MediaStore.Video.Media.DURATION 中的 DURATION 错误

转载 作者:行者123 更新时间:2023-11-29 20:53:28 26 4
gpt4 key购买 nike

我正在尝试在我的应用程序中录制视频,我注意到在显示它们的duration 时,我看到了错误的分钟\秒。只有通过以下代码录制的视频才会发生这种情况。通过其他应用录制的视频,时长显示在右侧:

 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;

}

这是我从视频中获取时长的方式:

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

最佳答案

这样解决:

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

需要计算视频时长并将其添加到 MediaStore 属性中。

关于android - MediaStore.Video.Media.DURATION 中的 DURATION 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28451571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com