时间 - 限制视频录制:在相机应用中显示最长时间

时间:2017-01-18 07:17:15

标签: android video android-intent camera

我正在开发一个需要视频捕捉的应用程序。

一切都很好:

  1. 打开视频录制相机。
  2. 正在录制视频20秒。
  3. 视频预览和一切。
  4. 问题:

    问题是,当我开始视频录制时,相机应用程序会在20秒后停止视频录制(这是正确的),但用户只会在20秒后收到通知"达到最大录制时间"。

    并在录制时显示:

    (重新编码时间)/(可记录的总时间)。

    我想要的是什么:

    我想要显示用户(剩余时间)/(20秒)。

    这是我的代码:

    public class MainActivity extends AppCompatActivity {
    
        private static final int REQUEST_VIDEO_CAPTURE = 1;
        private static final int MAXIMUM_VIDEO_RECORDING_LIMIT = 20;
        private static final int VIDEO_RECORDING_QUALITY = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            dispatchTakeVideoIntent();
        }
    
        private void dispatchTakeVideoIntent() {
    
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            takeVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, MAXIMUM_VIDEO_RECORDING_LIMIT);
            takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, VIDEO_RECORDING_QUALITY);
    
            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
    
                Uri file = data.getData();
    
            }
        }
    }
    

    如何实施,请帮助。

1 个答案:

答案 0 :(得分:0)

以下是使用MediaRecorder的简单视频录制示例:

在此,您可以定义视频录制的最大时间限制和文件大小。

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.main);

        SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        cameraView.setClickable(true);
        cameraView.setOnClickListener(this);
    }

    private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(holder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

    public void onClick(View v) {
        if (recording) {
            recorder.stop();
            recording = false;

            // Let's initRecorder so we can record again
            initRecorder();
            prepareRecorder();
        } else {
            recording = true;
            recorder.start();
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        prepareRecorder();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (recording) {
            recorder.stop();
            recording = false;
        }
        recorder.release();
        finish();
    }
}