android - Camera.open() occur error (using MediaRecorder) -
my action flow:
- camera.open()
- camera.unlock()
- recorder = new mediarecorder();
- recorder.setcamera(), , then.. more setting.
(click start button)
- recorder.start()
(click stop button)
- recorder.stop()
- recorder.release()
- recorder = null;
(save recorded video file)
- camera.open() <= occur error
step 9 recording again more video.
but, step occur error.
it's source :
public class talk extends activity implements surfaceholder.callback{
context context; surfaceview sfv_preview; progressbar progressbar; private surfaceholder holder; private mediarecorder recorder = null; camera camera; boolean recording = false; private static final string output_file = "/sdcard/vitalk/temp.mp4"; private static final string output_file_name = "temp.mp4"; private static final int recording_time = 10000; //uri fileuri; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_talk); file mediastoragedir = new file("/sdcard/vitalk/"); if (!mediastoragedir.exists()) mediastoragedir.mkdirs(); sfv_preview = (surfaceview) findviewbyid(r.id.sfv_preview); progressbar = (progressbar) findviewbyid(r.id.progressbar); holder = sfv_preview.getholder(); holder.addcallback(this); holder.settype(surfaceholder.surface_type_push_buffers); initrecorder(); context = this; ((imagebutton)findviewbyid(r.id.ibt_record)).setontouchlistener(new ontouchlistener(){ @override public boolean ontouch(view v, motionevent event) { if (motionevent.action_down == event.getaction()) { sfv_preview.setvisibility(view.visible); recorder.start(); recording = true; log.d("recod", "click start"); } else if (motionevent.action_up == event.getaction() || event.getaction() == motionevent.action_cancel) { log.d("recod", "click stop"); if (recording) { recorder.stop(); recording = false; recorder.release(); new handler().post(new runnable() { @suppresslint("newapi") @override public void run() { progressbar.setvisibility(view.visible); } }); uploadfile(output_file); recorder = null; initrecorder(); preparerecorder(); } } return false; } }); } @override public void surfacecreated(surfaceholder holder) { // todo auto-generated method stub preparerecorder(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub } @override public void surfacedestroyed(surfaceholder holder) { // todo auto-generated method stub } private void initrecorder() { camera camera = camera.open(1); camera.setdisplayorientation(90); camera.unlock(); recorder = new mediarecorder(); recorder.setcamera(camera); recorder.setaudiosource(mediarecorder.audiosource.mic); recorder.setvideosource(mediarecorder.videosource.camera); recorder.setprofile(camcorderprofile.get(camcorderprofile.quality_480p)); recorder.setoutputfile(output_file); recorder.setmaxduration(recording_time); recorder.setmaxfilesize(10485760); // approximately 5 megabytes } private void preparerecorder() { recorder.setpreviewdisplay(holder.getsurface()); recorder.setorientationhint(90); try { recorder.prepare(); //finish(); } catch (illegalstateexception e) { e.printstacktrace(); //finish(); } catch (ioexception e) { e.printstacktrace(); //finish(); } } public void uploadfile(string sourcefileuri) { string filename = sourcefileuri; httpurlconnection conn = null; dataoutputstream dos = null; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; file sourcefile = new file(sourcefileuri); try { // open url connection servlet fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url("http://133.130.117.56/visualtalk/func/fileupload.php"); // open http connection url conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); // allow inputs conn.setdooutput(true); // allow outputs conn.setusecaches(false); // don't use cached copy conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("uploaded_file", filename); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploaded_file\";filename=\"" + filename + "\"" + lineend); dos.writebytes(lineend); // create buffer of maximum size bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file , write form... bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } // send multipart form data necesssary after file data... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) int serverresponsecode = conn.getresponsecode(); string serverresponsemessage = conn.getresponsemessage(); log.i("uploadfile", "http response : " + serverresponsemessage + ": " + serverresponsecode); if(serverresponsecode == 200){ } //close streams // fileinputstream.close(); dos.flush(); dos.close(); } catch (malformedurlexception ex) { log.e("upload file server", "error: " + ex.getmessage(), ex); } catch (exception e) { log.e("upload file server exception", "exception : " + e.getmessage(), e); } new handler().post(new runnable() { @suppresslint("newapi") @override public void run() { sfv_preview.setvisibility(view.invisible); progressbar.setvisibility(view.invisible); } }); }
}
what problem?..
maybe should try use camera.release()
before next open()
Comments
Post a Comment