有没有办法获取正在进行的录制的持续时间

问题描述 投票:10回答:2

我正在尝试录制视频,并希望显示正在进行的录制的秒数。

我应该怎么做?

public void startRecording(View v){

    flipCamera.setVisibility(View.GONE);
    captureImage.setVisibility(View.GONE);
    String deviceMan = android.os.Build.MANUFACTURER;
    this.mediaRecorder = new MediaRecorder();
    this.mediaRecorder.setCamera(this.camera);

    camera.unlock();
    this.mediaRecorder.setCamera(camera);
    this.mediaRecorder.setOrientationHint(90);

    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile
            .get(CamcorderProfile.QUALITY_480P);
    this.mediaRecorder.setProfile(camcorderProfile_HQ);
    this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath());
    this.mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    this.mediaRecorder.setMaxFileSize(5000000);
    this.mediaRecorder.setPreviewDisplay(this.cameraPreview.getHolder()
            .getSurface());

     this.mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    try {
        this.mediaRecorder.prepare();
        // start the actual recording
        // throws IllegalStateException if not prepared
        this.mediaRecorder.start();
        Toast.makeText(this, R.string.recording, Toast.LENGTH_SHORT).show();

        this.toggleButtons(true);

    } catch (Exception e) {
        Log.wtf(TAG, "Failed to prepare MediaRecorder", e);
        Toast.makeText(this, R.string.cannot_record, Toast.LENGTH_SHORT)
                .show();
        this.releaseMediaRecorder();
    }
}

我对android很陌生,所以如果有人可以帮忙,请。

android video-recording
2个回答
13
投票

您可以使用计时器和处理程序来实现它。在下面的示例中,文本视图用于以00min:00sec格式显示持续时间。我在后台服务中使用此功能,但您也可以在活动中使用它。

public TextView timerTextView;
private long startHTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;


private Runnable updateTimerThread = new Runnable() {

            public void run() {

                timeInMilliseconds = SystemClock.uptimeMillis() - startHTime;

                updatedTime = timeSwapBuff + timeInMilliseconds;

                int secs = (int) (updatedTime / 1000);
                int mins = secs / 60;
                secs = secs % 60;  
                if (timerTextView != null)
                timerTextView.setText("" + String.format("%02d", mins) + ":"
                        + String.format("%02d", secs));
                customHandler.postDelayed(this, 0);
            }

        };

开始录制的地方:

      ......
          this.mediaRecorder.start()
          startHTime = SystemClock.uptimeMillis();
          customHandler.postDelayed(updateTimerThread, 0);

停止记录的位置:

     mediaRecorder.stop()
     timeSwapBuff += timeInMilliseconds;
 customHandler.removeCallbacks(updateTimerThread);

0
投票

您也可以使用Rxjava:

宣告可观察物:

    mTimeLoader = Observable.defer(() -> Observable.interval(0, AMPLITUDE_UPDATE_INTERVAL, TimeUnit.MILLISECONDS)
            .timeInterval()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());

激活:

    mTimeLoaderDisposable = mTimeLoader.subscribe(longTimed -> {
        mTotalMsRecorded = (int) (longTimed.value() * AMPLITUDE_UPDATE_INTERVAL);}

完成后,只需处置即可。

mTimeLoaderDisposable.dispose()
© www.soinside.com 2019 - 2024. All rights reserved.