- Home /
Android Unity, Screen Recording and MediaProjection APIs - UnityPlayerActivity hang.
I work on a project that uses MediaProjectionManager, MediaProjection and VirtualDisplay in order to record game play. This project also switches away from the UnityPlayerActivity and back every couple of minutes.
On the latest Unity, after starting my screen recording utility, I then switch to the UnityPlayerActivity:
activity.finish();
<Start Screen Recording>
activity.startActivity({Intent starting the UnityPlayerActivity});
<Call to UnitySendMessage triggering game scene to launch -> SceneManager.LoadScene(<scene name>)>
After doing this, the first time, the UnityPlayerActivity starts just fine, and my game switches from my native activity to my game scene and continues running. When game play finishes, I then switch back to my Native activity by calling static Java methods from UnityScript to trigger the change:
unityPlayerActivity.startActivity(<nativeActivity>)
My activity then starts up just fine.
However, when I go to switch back to the UnityPlayerActivity a second time, it has hung. All rendering stops, and no messages I send it are received. This hang happens no matter which scene I try to trigger, whether or not I try to close out my screen recording utility, and whether or not I try to start my screen recording again on this second switch.
At this point, I've narrowed it down to the screen recording via trial and error removal of code, as no exceptions are thrown or logged. I'm fairly certain that the creation of the VirtualDisplay and MediaProjection are causing this issue.
mMediaProjectionManager = (MediaProjectionManager) unityPlayerActivity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
mVirtualDisplay = mMediaProjection.createVirtualDisplay(
"recorder",
mStream.getVideoQuality().resX,
mStream.getVideoQuality().resY,
DisplayMetrics.DENSITY_LOW,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface,
null, // callback
null // handler
);
Removing the creation of these from my screen recording utility clears up all of my problems, as does removing the screen recording utility entirely. This worked fine on Unity 4.x, but upon updating my project to 5.4, it's 100% reproducible.
Would anyone have any ideas on what could be going wrong, or how I could debug this issue?
Did you find a solution to your problem? I have a similar one and found out that the Unity application never receives the OnApplicationPause(true) message after giving permission for screen capture but I have no idea why this happens
Your answer
Follow this Question
Related Questions
NativePlugin c++ Android sample and self build pcl not loading,Native Plugins c++ "dll not found" 2 Answers
How to save screenshots on Android with Unity and C#? 0 Answers
How to save an in game screenshot directly to gallery? 1 Answer
Continue developing in Android Studio 1 Answer
Normal map is not working with Unity 5 when Low-level Native Plugin Interface is used 0 Answers