时差检查 - Android视频捕捉

时间:2015-12-16 17:21:48

标签: android android-studio android-timer

我正在制作视频录制相机APP。如果相机在启动后立即停止可能是因为视频尺寸非常小,应用程序崩溃。我只想在视频大小超过1秒时激活停止按钮。但问题是我无法正确找到当前时间和开始时间。找到两个时间因素的差异将有助于实现2秒检查。需要帮助。

private void onClickActions(View v)
{
    float tt = start_time /10000000000000f;
    float ct = ((System.currentTimeMillis() ) /10000000000000f);

    Log.d("Before stoping S-Time ",tt+"");
    Log.d("Before stoping C-Time ",ct+"");

    if (recording  && tt>=2.0f)
     {
         Log.d("After Stopping = ",tt+"");
        // stop recording and release camera
        mediaRecorder.stop(); // stop the recording
        recording = false;
        rec.setVisibility(View.INVISIBLE);
        start_time = 0;

    }

    //remove time code to initial revert
    if(v.getId()== start.getId() && ((CameraPreview.recordHappy || CameraPreview.recordSad))) {
        prepareMediaRecorder();
        recording = true;
        mediaRecorder.start();
        consent = true;
        happyRecorded=true;
        stop.setClickable(true);
        start.setClickable(false);

        if (AndroidVideoCaptureExample.iV.getVisibility()==View.VISIBLE)
        AndroidVideoCaptureExample.iV.setVisibility(View.INVISIBLE);
        //AndroidVideoCaptureExample.capture.setText("RECORDING STARTED!");

        rec.setVisibility(View.VISIBLE);
        start_time = (int)(System.currentTimeMillis());
        //Toast.makeText(myContext, "You are being recorded now!", Toast.LENGTH_LONG);

    }
    if(v.getId()== stop.getId() && consent==true && recording==false) {

        if((!CameraPreview.recordHappy && CameraPreview.recordSad))
        {
            releaseMediaRecorder(); // release the MediaRecorder object
            Intent intent = new Intent();
            intent.setClass(AndroidVideoCaptureExample.this, consentActivity.class);
            startActivity(intent);
            finish();
        }
        else {
            CameraPreview.recordHappy = false;
            CameraPreview.recordSad = true;
            stop.setClickable(false);
            start.setClickable(true);
            recording = false;
            AndroidVideoCaptureExample.capture.setText("Record Neutral Moment");
            rec.setVisibility(View.INVISIBLE);

        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你可能会过度设计一件简单的事情。除非您在UI上显示记录时间,否则您不需要计算记录时间。如果要禁用该按钮,只需在开始录制之前将其禁用,然后在2秒后使用Handler重新启用:

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                // enable stop button
            }
        },2000);

但是,我认为这不是一个非常好的用户体验。如果你看一下像谷歌相机这样的相机,你可以在启动后立即停止它,它不会记录任何东西。要实现此目的,您需要在调用RuntimeException时捕获mediaRecorder.stop(),然后检查并清理生成的文件。如果它为空,则删除它,不要向UI抛出错误。