Android MediaRecorder is throwing IllegalStateException -
i trying develop simple android video recorder. have created custom surfaceview
mediarecoder
handle recording. builds fine , runs fine on android device. when call startrecording()
, giving me illegalstateexception
. here code:
package my.android.video; import android.media.mediarecorder; import android.view.surfaceview; import android.view.surfaceholder; import android.util.log; import java.lang.illegalargumentexception; import java.lang.illegalstateexception; import java.io.ioexception; import android.content.context; public class recorderview extends surfaceview implements surfaceholder.callback { private mediarecorder mediarecorder; public recorderview(context context) { super(context); surfaceholder holder = getholder(); holder.addcallback(this); holder.settype(surfaceholder.surface_type_push_buffers); holder.setfixedsize(400, 300); } @override public void surfacecreated(surfaceholder holder) { if (mediarecorder == null) { try { mediarecorder=new mediarecorder(); mediarecorder.setaudiosource(mediarecorder.audiosource.mic); mediarecorder.setvideosource(mediarecorder.videosource.camera); mediarecorder.setoutputformat(mediarecorder.outputformat.default); mediarecorder.setaudioencoder(mediarecorder.audioencoder.default); mediarecorder.setvideoencoder(mediarecorder.videoencoder.default); mediarecorder.setoutputfile("/sdcard/myoutputfile.mp4"); mediarecorder.setpreviewdisplay(holder.getsurface()); mediarecorder.prepare(); } catch (illegalargumentexception e) { log.d("media_player", e.getmessage()); } catch (illegalstateexception e) { log.d("media_player", e.getmessage()); } catch (ioexception e) { log.d("media_player", e.getmessage()); } } } @override public void surfacedestroyed(surfaceholder holder) { mediarecorder.release(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo } public void startrecording() { mediarecorder.start(); } public void stoprecording() { mediarecorder.stop(); } }
edit
logcat output
i/activitymanager( 107): starting activity: intent { act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x10200000 cmp=my.android.video/.videorecorder } i/activitymanager( 107): start proc my.android.video activity my.android.video/.videorecorder: pid=1534 uid=10098 gids={1015} i/windowmanager( 107): setting rotation 1, animflags=1 i/activitymanager( 107): config changed: { scale=1.0 imsi=310/120 loc=en_us touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uimode=17 seq=18} w/inputmanagerservice( 107): ignoring hidesoftinput of: com.android.internal.view.iinputmethodclient$stub$proxy@4620e0d8 i/windowmanager( 107): setting rotation 0, animflags=0 i/activitymanager( 107): config changed: { scale=1.0 imsi=310/120 loc=en_us touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uimode=17 seq=19} i/usagestats( 107): unexpected resume of my.android.video while resumed in my.android.video i/windowmanager( 107): setting rotation 1, animflags=0 i/activitymanager( 107): config changed: { scale=1.0 imsi=310/120 loc=en_us touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uimode=17 seq=20} i/usagestats( 107): unexpected resume of my.android.video while resumed in my.android.video e/androidruntime( 1534): fatal exception: main e/androidruntime( 1534): java.lang.illegalstateexception e/androidruntime( 1534): @ android.media.mediarecorder.start(native method) e/androidruntime( 1534): @ my.android.video.recorderview.startrecording(recorderview.java:62) e/androidruntime( 1534): @ my.android.video.videorecorder$1.onclick(videorecorder.java:40) e/androidruntime( 1534): @ android.view.view.performclick(view.java:2408) e/androidruntime( 1534): @ android.view.view$performclick.run(view.java:8817) e/androidruntime( 1534): @ android.os.handler.handlecallback(handler.java:587) e/androidruntime( 1534): @ android.os.handler.dispatchmessage(handler.java:92) e/androidruntime( 1534): @ android.os.looper.loop(looper.java:144) e/androidruntime( 1534): @ android.app.activitythread.main(activitythread.java:4937) e/androidruntime( 1534): @ java.lang.reflect.method.invokenative(native method) e/androidruntime( 1534): @ java.lang.reflect.method.invoke(method.java:521) e/androidruntime( 1534): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) e/androidruntime( 1534): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) e/androidruntime( 1534): @ dalvik.system.nativestart.main(native method) w/activitymanager( 107): force finishing activity my.android.video/.videorecorder w/activitymanager( 107): activity pause timeout historyrecord{462016f0 my.android.video/.videorecorder} i/windowmanager( 107): setting rotation 0, animflags=1 i/activitymanager( 107): config changed: { scale=1.0 imsi=310/120 loc=en_us touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uimode=17 seq=21} d/pendingintent( 236): pendingintent getbroadcast intent string =com.htc.htccalendarwidgets.mywidgetview4110120 d/pendingintent( 236): pendingintent getbroadcast returnpendingintent packagename =com.htc.launcher v/alarmmanager( 107): adding alarm{4614d4d0 type 0 com.htc.launcher} feb 24 12:00:00 i/windowmanager( 107): win death: window{462ec3b0 my.android.video/my.android.video.videorecorder paused=false} i/activitymanager( 107): process my.android.video (pid 1534) has died. w/activitymanager( 107): activity destroy timeout historyrecord{462016f0 my.android.video/.videorecorder}
i not able figure out problem new android programming.
the problem can parameters passing preview surface. in line:
holder.setfixedsize(400,300)
try passing other parameters eg. (320, 240)
also, make sure preview surface accessible, before call setpreviewsurface made.
Comments
Post a Comment