camera - Mediarecorder.start() RuntimeException: start failed android 4.0.4 -
tried make camera app , have followed googles developer guide, runtimeexception: start failed on android 4.0.4 device. works on friends 4.2.x samsung. tried google problem haven't found relevant solution.
error code
05-13 18:16:06.458: e/mediarecorder(32701): start failed: -19 05-13 18:16:06.468: e/androidruntime(32701): java.lang.runtimeexception: start failed. 05-13 18:16:06.468: e/androidruntime(32701): @ android.media.mediarecorder.start(native method) 05-13 18:16:06.468: e/androidruntime(32701): @ com.example.cameeera.main.onclick(main.java:61)
main class
import java.io.ioexception; import android.app.activity; import android.hardware.camera; import android.media.camcorderprofile; import android.media.mediarecorder; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.framelayout; public class main extends activity implements onclicklistener{ private camera mcamera; private camerapreview mpreview; private mediarecorder mmediarecorder; private boolean isrecording = false; button capturebutton; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); capturebutton = (button) findviewbyid(r.id.button_capture); capturebutton.setonclicklistener(this); // create instance of camera mcamera = getcamerainstance(); // create our preview view , set content of our activity. mpreview = new camerapreview(this, mcamera); framelayout preview = (framelayout) findviewbyid(r.id.camera_preview); preview.addview(mpreview); } @override public void onclick(view v) { if(v.getid() == r.id.button_capture){ if (isrecording) { // stop recording , release camera mmediarecorder.stop(); // stop recording releasemediarecorder(); // release mediarecorder object mcamera.lock(); // take camera access mediarecorder // inform user recording has stopped capturebutton.settext("start");; isrecording = false; } else { // initialize video camera if (preparevideorecorder()== true) { // camera available , unlocked, mediarecorder prepared, // can start recording mmediarecorder.start(); // inform user recording has started capturebutton.settext("stop"); isrecording = true; } else { // prepare didn't work, release camera releasemediarecorder(); // inform user } } } }//onclick private boolean preparevideorecorder(){ //mcamera = getcamerainstance(); mmediarecorder = new mediarecorder (); // step 1: unlock , set camera mediarecorder mcamera.unlock(); mmediarecorder.setcamera(mcamera); // step 2: set sources mmediarecorder.setaudiosource(mediarecorder.audiosource.camcorder); mmediarecorder.setvideosource(mediarecorder.videosource.camera); // step 3: set camcorderprofile (requires api level 8 or higher) //mmediarecorder.setprofile(camcorderprofile.get(camcorderprofile.quality_high)); camcorderprofile highcameraprofile = camcorderprofile.get(camcorderprofile.quality_high); mmediarecorder.setprofile(highcameraprofile); mmediarecorder.setmaxduration(100000); // maxduration 10 seconds mmediarecorder.setmaxfilesize(20000000); // maxsize 20 megabytes // step 4: set output file mmediarecorder.setoutputfile(savevideofile.getoutputmediafile(savevideofile.media_type_video).tostring()); // step 5: set preview output mmediarecorder.setpreviewdisplay(mpreview.getholder().getsurface()); // step 6: prepare configured mediarecorder try { mmediarecorder.prepare(); } catch (illegalstateexception e) { log.d("tag", "illegalstateexception preparing mediarecorder: " + e.getmessage()); releasemediarecorder(); return false; } catch (ioexception e) { log.d("tag", "ioexception preparing mediarecorder: " + e.getmessage()); releasemediarecorder(); return false; } return true; }//preparevideorecorder @override protected void onpause() { super.onpause(); releasemediarecorder(); // if using mediarecorder, release first releasecamera(); // release camera on pause event } private void releasemediarecorder(){ if (mmediarecorder != null) { mmediarecorder.reset(); // clear recorder configuration mmediarecorder.release(); // release recorder object mmediarecorder = null; mcamera.lock(); // lock camera later use } } private void releasecamera(){ if (mcamera != null){ mcamera.release(); // release camera other applications mcamera = null; } } public static camera getcamerainstance(){ camera c = null; try { c = camera.open(); // attempt camera instance } catch (exception e){ // camera not available (in use or not exist) log.d("tag", "camera not available (in use or not exist)" + e.getmessage()); } return c; // returns null if camera unavailable } }
here camerapreview class, works fine package com.example.cameeera;
import java.io.ioexception; import android.content.context; import android.hardware.camera; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; /** camera preview class */ public class camerapreview extends surfaceview implements surfaceholder.callback { public surfaceholder mholder; private camera mcamera; public camerapreview(context context, camera camera) { super(context); mcamera = camera; // install surfaceholder.callback notified when // underlying surface created , destroyed. mholder = getholder(); mholder.addcallback(this); } public void surfacecreated(surfaceholder holder) { // surface has been created, tell camera draw preview. try { mcamera.setpreviewdisplay(holder); mcamera.startpreview(); } catch (ioexception e) { log.d("tag", "error setting camera preview: " + e.getmessage()); } } public void surfacedestroyed(surfaceholder holder) { // empty. take care of releasing camera preview in activity. } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // if preview can change or rotate, take care of events here. // make sure stop preview before resizing or reformatting it. if (mholder.getsurface() == null){ // preview surface not exist return; } // stop preview before making changes try { mcamera.stoppreview(); } catch (exception e){ // ignore: tried stop non-existent preview } // set preview size , make resize, rotate or // reformatting changes here // start preview new settings try { mcamera.setpreviewdisplay(mholder); mcamera.startpreview(); } catch (exception e){ log.d("tag", "error starting camera preview: " + e.getmessage()); } } }
Comments
Post a Comment