Wrong DURATION in MediaStore.Video.Media.DURATION

你离开我真会死。 提交于 2019-12-02 11:22:29

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28451571/wrong-duration-in-mediastore-video-media-duration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!