This article mainly uses the intent to trigger the built-in Camera application to record videos.
Source code:
Layout file:
Activity_main:
Code file:
MainActivity:
Package com. multimediademo14intentcamera; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. videoView;/*** use intent to record video **/public class MainActivity extends Activity implements OnClickListener {public static int VIDEO_CAPTURED = 1; /*** Two buttons: video recording Button and video playback Button */private Button button_capture, button_play; private VideoView videoView; private Uri videoFileUri; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button_capture = (Button) findViewById (R. id. button_capture); button_play = (Button) findViewById (R. id. button_play); videoView = (VideoView) fin DViewById (R. id. videoView); listener (this); button_play.setOnClickListener (this); button_play.setEnabled (false) ;}@ Overridepublic void onClick (View v) {if (v = button_capture) {/*** the video recording button of the store, which passes the creation intent together with the VIDEO_CAPTURED constant to the startActivityForResult method. * This starts the built-in Camera application. */Intent captureVideoIntent = new Intent (android. provider. mediaStore. ACTION_VIDEO_CAPTURE); startActivityForResult (captureVideoIntent, VIDEO_CAPTURED);} else if (v = button_play) {videoView. setVideoURI (videoFileUri); videoView. start () ;}}/*** when the Camera application returns, the following onActivityResult method is called. It checks whether the resultCode is the constant RESULT_ OK, * and whether the requestCode is the value passed into startActivityForResult VIDEO_CAPTURED *, and then obtains the Uri of the recorded video file. * // @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = RESULT_ OK & requestCode = VIDEO_CAPTURED) {videoFileUri = data. getData (); button_play.setEnabled (true );}}}
Source code download:
Click to download source code