Get the App
SLTechnology News&Howtos  ›  Development  › 

How to limit the recording time by calling the camera of mobile phone in Android

Shulou Source: shulou.com Published: 2022-06-01 03:03:00 09月27日 Update

Editor to share with you how Android can use mobile phone camera to limit the length of video recording. I hope you will get something after reading this article. Let's discuss it together.

The details are as follows

Because the server space is limited, the duration of the video must be limited.

First lay out a button in xml and click to start recording. Layout a TextView to display the countdown time.

1. Use Hander+TimerTask to complete timing operation

Private TextView btn_stop;private int recLen = 11 timer timer = new Timer (); / / the countdown is placed after the start button event, see heading 7 where the countdown begins / / timer.schedule (task,1000,1000) Final Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg.what) {case 1: btn_stop.setText ("" + recLen); if (recLen 2 * 1024 * 1024) {mediaRecorder.setVideoEncodingBitRate (2 * 1024 * 1024);} else {mediaRecorder.setVideoEncodingBitRate (1024 * 1024) } mediaRecorder.setVideoFrameRate (mProfile.videoFrameRate); mediaRecorder.setVideoSize (1280); mediaRecorder.setOutputFile (mVecordFile.getAbsolutePath ());}

5. Stop recording

Private void stopRecord () {try {if (isRecording & & mediaRecorder! = null) {mediaRecorder.setOnErrorListener (null); mediaRecorder.setPreviewDisplay (null); mediaRecorder.stop (); mediaRecorder.reset (); mediaRecorder.release (); mediaRecorder = null; isRecording = false Log.i (TAG, "" + mVecordFile.toString ()); / / new imageTask (). Execute (mVecordFile); MessageData md = new MessageData (); md.srdatatype = SRDataType. Video is returned; md.Data = mVecordFile.getAbsolutePath (); Intent intent = new Intent (BroadCastParameter.action); intent.putExtra ("data", md); sendBroadcast (intent); / / Fragment page broadcast notification LocalBroadcastManager.getInstance (CustomRecorder.this) .sendbroadcast (intent); finish () } catch (Exception ex) {/ / Toast.makeText (getApplicationContext (), ex.getMessage (), 1). Show ();}}

Turn off the camera

Private void stopCamera () {if (mCamera! = null) {mCamera.setPreviewCallback (null); mCamera.stopPreview (); mCamera.release (); mCamera = null;}}

Click the start recording button

Public void onViewClicked (View view) {if (Build.VERSION.SDK_INT > = 23) {int REQUEST_CODE_CONTACT = 101 / / verify whether for (String str: permissions) {if (this.checkSelfPermission (str)! = PackageManager.PERMISSION_GRANTED) {Toast.makeText (getApplicationContext (), "Storage permission is not authorized", 1) .show (); return } switch (view.getId ()) {/ / start case R.id.btn_start: btn_start.setVisibility (View.GONE); btn_stop.setVisibility (View.VISIBLE) / / this is to determine whether the video file has been created. If not, return boolean creakOk = createRecordDir (); if (! creakOk) {return;} try {mCamera.unlock (); setConfigRecord () MediaRecorder.prepare (); mediaRecorder.start ();} catch (Exception e) {/ / Toast.makeText (getApplicationContext (), e.getMessage (), Toast.LENGTH_LONG). Show ();} StartTime = new Date (System.currentTimeMillis ()); isRecording = true CustomTimer.ReStart (); timer.schedule (task,1000,1000); break; has read this article. I believe you have a certain understanding of "how to use mobile phone camera to limit the length of video recording". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Tags: Video camera camera time countdown video duration limit button format support mobile phone resolution image layout file time permission article encoding Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi MySQL Shulou Technology Redmi