java - MediaRecorder.GetSurface() returning null -
i using code screen recording on nexus 5 running 6.0.1 july security update. screen recording works fine on other devices running 5.0.1, 6.0, 6.0.1 it's not working on phone. gives me following error when try start screen recording.
e/mediarecorder: surfacemediasource not initialized! e/androidruntime: fatal exception: main java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1995, result=-1, data=intent { (has extras) }} activity {mainactivity}: java.lang.illegalstateexception: failed surface @ android.app.activitythread.deliverresults(activitythread.java:3699) caused by: java.lang.illegalstateexception: failed surface @ android.media.mediarecorder.getsurface(native method)
it's failing surface screen recording. what's causing , how can resolve this?
source code:
public static mediaprojectionmanager getmmediaprojectionmanager(final mainactivity context) { displaymetrics metrics = new displaymetrics(); context.getwindowmanager().getdefaultdisplay().getmetrics(metrics); mscreendensity = metrics.densitydpi; display_height = metrics.heightpixels; display_width = metrics.widthpixels; mmediarecorder = new mediarecorder(); mmediaprojectionmanager = (mediaprojectionmanager) context.getsystemservice(context.media_projection_service); return mmediaprojectionmanager; } @targetapi(21) public static void startscreenrecording(intent data) { mmediaprojectioncallback = new mediaprojectioncallback(); initrecorder(null); mmediaprojection = mmediaprojectionmanager.getmediaprojection(result_ok, data); mmediaprojection.registercallback(mmediaprojectioncallback, null); mvirtualdisplay = createvirtualdisplay(); mmediarecorder.start(); } @targetapi(21) private static virtualdisplay createvirtualdisplay() { return mmediaprojection.createvirtualdisplay("mainactivity", display_width, display_height, mscreendensity, displaymanager.virtual_display_flag_auto_mirror, mmediarecorder.getsurface(), null /*callbacks*/, null /*handler*/); } @targetapi(21) private static void initrecorder(mainactivity context) { try { mmediarecorder.setaudiosource(mediarecorder.audiosource.mic); mmediarecorder.setvideosource(mediarecorder.videosource.surface); mmediarecorder.setoutputformat(mediarecorder.outputformat.mpeg_4); mmediarecorder.setoutputfile(environment .getexternalstoragedirectory() + "/video"+ system.currenttimemillis()+".mp4"); mmediarecorder.setvideosize(display_width, display_height); mmediarecorder.setvideoencoder(mediarecorder.videoencoder.h264); mmediarecorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); mmediarecorder.setvideoencodingbitrate(video_encoding_bitrate); mmediarecorder.setvideoframerate(video_frame_rate); mmediarecorder.prepare(); } catch (exception e) { log.e("util", e.getlocalizedmessage()); } } @targetapi(21) private static class mediaprojectioncallback extends mediaprojection.callback { @override public void onstop() { } } @targetapi(21) public static void stopscreensharing() { mmediarecorder.stop(); mmediarecorder.reset(); if (mvirtualdisplay == null) { return; } mvirtualdisplay.release(); destroymediaprojection(); } @targetapi(21) private static void destroymediaprojection() { if (mmediaprojection != null) { log.e(tag, "destroying projection"); mmediaprojection.unregistercallback(mmediaprojectioncallback); mmediaprojection.stop(); mmediaprojection = null; } }
best regards
this same question this one. i'm facing problem. strange thing it happens on marshmallow, on lollipop work.
the documentaion says:
surface getsurface ()
may called after prepare. frames rendered surface before start discarded. throws:
illegalstateexception - if called before prepare, after stop, or called when videosource not set surface.
but in mediarecorder.java, is:
@throws illegalstateexception if called after {@link #prepare} , before {@link #stop}.
however doesn't make difference whether i'm placing before or after prepare()
, both not work. it's weird throws illegalstateexception
although none of above things apply.
however, this solution matt snider work on marshmallow. since more difficult imo (especially when trying record audio well) great running mediarecorder
.
if wants reproduce problem use this or this code , run on marshmallow machine.
Comments
Post a Comment