This article will primarily record video by intent to trigger the built-in camera application.
Source:
Layout file:
Activity_main:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Butt On android:id= "@+id/button_capture" android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "android:layout_alignparentleft=" true "android:layout_alignparenttop=" true "android:text=" recording video "/> <button android:id=" @+id/button_play "android:layout_width=" Match_parent "android:layou t_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_below= "@+id/button_capture" android:text= "Play video"/> <videoview android:id= "@+id/videoview" android:layout_width= "Wrap_conten T "android:layout_height=" Wrap_content "android:layout_alignparentleft=" true "android:layout_below=" @ +id/button_play "/></relativelayout>
Code files:
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 impl Ements Onclicklistener {public static int video_captured = 1;/** * Two buttons: Record video button, Play video button */private button button_capture, BU Tton_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) Findviewbyid (R.id.videoview); Button_capture.setonclicklistener (this); Button_play.setonclicklistener ( this); button_play.setenabled (false);} @Overridepublic void OnClick (View v) {if (v = = button_capture) {/** * Store Record a video button that will create the intent and pass it along with the video_captured constant to the Startactivityforresult method, * This will launch 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 ResultCode is a constant RESULT_OK, * and whether Requestcode is the value passed in to Startactivityforresult video_captured *, and then gets 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 the source code