Android video recording problems

Source: Internet
Author: User
Recording videos with Android today

Use someone else off-the-shelf code, code original address http://blog.csdn.net/peijiangping1989/article/details/7049991

However, an error is reported.

06-03 21:32:32. 285: W/system. Err (27250): Java. Lang. runtimeexception: Start failed.
06-03 21:32:32. 285: W/system. Err (27250): at Android. Media. mediarecorder. Start (native method)
06-03 21:32:32. 285: W/system. Err (27250): At com. it_videorecorder.mainactivity.record (mainactivity. Java: 87)
06-03 21:32:32. 285: W/system. Err (27250): At java. Lang. Reflect. method. invokenative (native method)
06-03 21:32:32. 285: W/system. Err (27250): At java. Lang. Reflect. method. Invoke (method. Java: 511)
06-03 21:32:32. 285: W/system. Err (27250): at Android. View. View $1. onclick (view. Java: 3586)
06-03 21:32:32. 285: W/system. Err (27250): at Android. View. View. Adjust mclick (view. Java: 4084)
06-03 21:32:32. 285: W/system. Err (27250): at Android. View. View $ invoke mclick. Run (view. Java: 16966)
06-03 21:32:32. 285: W/system. Err (27250): at Android. OS. handler. handlecallback (handler. Java: 615)
06-03 21:32:32. 289: W/system. Err (27250): at Android. OS. handler. dispatchmessage (handler. Java: 92)
06-03 21:32:32. 289: W/system. Err (27250): at Android. OS. low.loop (low.java: 137)
06-03 21:32:32. 289: W/system. Err (27250): at Android. App. activitythread. Main (activitythread. Java: 4931)
06-03 21:32:32. 289: W/system. Err (27250): At java. Lang. Reflect. method. invokenative (native method)
06-03 21:32:32. 289: W/system. Err (27250): At java. Lang. Reflect. method. Invoke (method. Java: 511)
06-03 21:32:32. 289: W/system. Err (27250): At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 791)
06-03 21:32:32. 292: W/system. Err (27250): At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 558)
06-03 21:32:32. 292: W/system. Err (27250): At Dalvik. system. nativestart. Main (native method)

 

This error has been reported by Baidu, and many people may have encountered it.

I have read about several possible reasons.

The size setting of the first video is not supported.

The second format is incorrectly set. If it is 3GP or MP4, different mobile phones may have different implementation methods. This is my reason.

The third frame rate is set incorrectly. The frame rate setting of a high mobile phone may not support such a high resolution.

This is probably because each program is different, even if the same error is reported, the solution will be different. Try all of them one by one.

Post a code, because the code in the above link will have a small error. Send me the code and take it as useful.

Package COM. dineed. videorecorder; import android. OS. bundle; import android. app. activity; import android. view. menu; import Java. io. ioexception; import javax. security. auth. callback. callback; import android. content. PM. activityinfo; import android. graphics. pixelformat; import android. media. mediarecorder; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceview; import and Roid. view. view; import android. view. view. onclicklistener; import android. view. window; import android. view. windowmanager; import android. widget. button; public class videorecorderactivity extends activity {private button start; // start recording button private button stop; // stop recording button private mediarecorder; // class private surfaceview of the recorded video; // The video display control // an interface used to display the video. I don't need it anymore. That is to say, I use mediarecorder to record the video and You have to watch a video on the page // you can consider other methods .. Well, you need to implement the callback interface private surfaceholder; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); // remove the title bar getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // sets full screen // sets setrequestedorientation (activityinfo. screen_orientation_landscap E); // select semi-transparent mode, which can be used in a surfaceview activity. Getwindow (). setformat (pixelformat. translucent); setcontentview (R. layout. activity_video_recorder); Init ();} private void Init () {start = (button) This. findviewbyid (R. id. start); stop = (button) This. findviewbyid (R. id. stop); start. setonclicklistener (New testvideolistener (); stop. setonclicklistener (New testvideolistener (); surfaceview = (surfaceview) This. findviewbyid (R. id. surfaceview); surfacehold Er holder = surfaceview. getholder (); // get Holder. addcallback (New surfaceholder. callback () {@ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stubsurfaceview = NULL; surfaceholder = NULL; mediarecorder = NULL ;} @ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stubsurfaceholder = holder;} @ overridepublic void surfa Cechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stubsurfaceholder = holder ;}}); // The callback interface added by holder // settype must be set to avoid errors. holder. settype (surfaceholder. surface_type_push_buffers);} @ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. video_recorder, Menu); Return true;} class testvideolistener implements onclicklistener {@ override public void onclick (view v) {If (V = Start) {mediarecorder = new mediarecorder (); // create a mediarecorder object // set the recorded video source to camera (CAMERA) mediarecorder. setvideosource (mediarecorder. videosource. camera); // set the video Encapsulation Format three_gpp to 3GP after recording. mpeg_4 is MP4 mediarecorder. setoutputformat (mediarecorder. outputformat. mpeg_4); // sets the recorded video encoding h263 H264 mediarecorder. setvideoencoder (mediarecorder. videoencoder. mpeg_4_sp); // sets the video recording resolution. It must be placed behind the encoding and format. Otherwise, the error mediarecorder. setvideosize (640,480) is returned. // you can specify the recorded video frame rate. It must be placed behind the encoding and format. Otherwise, an error is returned. // mediarecorder. setvideoframerate (20); mediarecorder. setpreviewdisplay (surfaceholder. getsurface (); // sets the video file output path mediarecorder. setoutputfile ("/sdcard/love.3gp"); try {// prepare the recording mediarecorder. prepare (); // start recording mediarecorder. start ();} catch (illegalstateexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch Block E. printstacktrace () ;}} if (V = stop) {If (mediarecorder! = NULL) {// stop recording mediarecorder. stop (); // release the resource mediarecorder. release (); mediarecorder = NULL ;}}} public void surfacechanged (surfaceholder holder, int format, int width, int height) {// set holder, this holder is the holder that was obtained in oncreat and assigned it to surfaceholder = holder;} public void surfacecreated (surfaceholder holder) {// change holder, this holder is the holder that was obtained in oncreat and assigned it to surfaceholder = holder;} public void surfacedestroyed (surfaceholder holder) {// when surfacedestroyed is set, the object is also set to null surfaceview = NULL; surfaceholder = NULL; mediarecorder = NULL ;}}

Layout File

It is actually a surfview and two buttons.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1" >        <SurfaceView            android:id="@+id/surfaceview"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="4"        android:gravity="center"        android:orientation="vertical" >        <Button            android:id="@+id/start"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Start" />        <Button            android:id="@+id/stop"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Stop" />    </LinearLayout></LinearLayout>

 

You must add the write permission for external storage and recording permission for camera in manifest. xml.

<Uses-Permission Android: Name = "android. Permission. Camera"/>
<Uses-Permission Android: Name = "android. Permission. record_audio"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.