如何解决MediaRecorder.getSurface() 终止前台服务
我正在尝试从前台服务获取屏幕录制工作,媒体录制器调用此方法:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void startRecording(int resultCode,Intent data) {
Log.v(TAG,"Starting recording...");
MediaProjectionManager mProjectionManager = (MediaProjectionManager) getApplicationContext().getSystemService (Context.MEDIA_PROJECTION_SERVICE);
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setOnErrorListener(this::onRecorderError);
mMediaRecorder.setOnInfoListener(this::onRecorderInfo);
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getRealMetrics(metrics);
int mScreenDensity = metrics.densityDpi;
int displayWidth = metrics.widthPixels;
int displayHeight = metrics.heightPixels;
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(8 * 1000 * 1000);
mMediaRecorder.setVideoFrameRate(15);
mMediaRecorder.setVideoSize(displayWidth,displayHeight);
Log.v(TAG,"Recorder parameters done");
String videoDir = Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES).getAbsolutePath();
Long timestamp = System.currentTimeMillis();
String orientation = "portrait";
if( displayWidth > displayHeight ) {
orientation = "landscape";
}
String filePathAndName = videoDir + "/time_" + timestamp.toString() + "_mode_" + orientation + ".mp4";
mMediaRecorder.setOutputFile( filePathAndName );
Log.v(TAG,"Recorder output path done,prepairing...");
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.v(TAG,"Recorder prepaired");
Surface surface = mMediaRecorder.getSurface(); //Stops here
Log.v(TAG,"Surface ready");
mMediaProjection = mProjectionManager.getMediaProjection(resultCode,data);
Log.v(TAG,"Media projection ready");
mVirtualDisplay = mMediaProjection.createVirtualDisplay("MainActivity",displayWidth,displayHeight,mScreenDensity,VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,surface,null,null);
Log.v(TAG,"Display ready");
mMediaRecorder.start();
Log.v(TAG,"Started recording");
}
但是当我调用 mMediaRecorder.getSurface() 时,活动完全停止,没有异常,没有来自 mMediaRecorder 的回调,甚至没有调用我的服务的 onDestory(),但应用程序本身一直在运行
就好像服务完全停止工作一样
我做错了什么?我正在 android 9.1.0.181C10 HUAWEI LLD-L31 上测试
解决方法
不完全是一个,但我在调查 MediaCodec 周围奇怪崩溃的原因时可能遇到过类似的问题,这种崩溃只发生在一些华为、三星和摩托罗拉设备上。
长话短说,这是因为懒惰。 MediaCodec 需要时间在这些设备上做好准备。试图让它运行得太快,底层系统模块陷入无限循环并被强制终止。
如果您发现 LogCat 耗尽了一些包含“libstagefright_foundation.so”或“__ubsan_handle_mul_overflow_minimal_abort”的行,那就是。然后,尝试插入一点延迟,例如在 MediaRecorder.prepare() 之后的 Thread.sleep(100) 以解决问题。
,原来问题出在应用权限上。由于我多次删除和安装它,应用程序权限被重置,应用程序失去了文件访问权限。当我在设置中启用它时,它按预期工作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。