android 调用 screenrecord 实现录屏

首先要说明的是并未实现,本文讲一下自己的思路。
adb 使用shell 命令 screenrecord 可录屏。
自己写了个app,通过Process p = Runtime.getRuntime().exec(cmd)的方式调用shell命令,报错:

java.lang.SecurityException: Permission Denial: broadcast asks to run as user -2 but is calling from user 0

需要android.permission.INTERACT_ACROSS_USERS_FULL 或者 android.permission.INTERACT_ACROSS_USERS 权限,而这个权限是system app的权限,第三方app是没有权限申请的。

所以说4.4的录屏是需要root权限的。5.0 之后的 MediaProjection API 不需要 root权限(which allows ordinary, unprivileged applications to record the screen)。

使用 verbose 参数,可见录屏结束后会发送一个广播,用于告诉系统有新文件产生了:

shell@aries:/sdcard $ screenrecord --verbose --time-limit 10 /sdcard/1.mp4
Main display is 720x1280 @59.00fps (orientation=0)
Configuring recorder for 720x1280 video/avc at 4.00Mbps
Content area is 720x1280 at offset x=0 y=0
Time limit reached
Encoder stopping; recorded 6 frames in 10 seconds
Stopping encoder and muxer
Executing: /system/bin/am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_
FILE -d file:///sdcard/1.mp4
Broadcasting: Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE dat=fil
e:///sdcard/1.mp4 }
Broadcast completed: result=0

既然如此看一下screenrecord源码吧。

frameworks\av\cmds\screenrecord\screenrecord.cpp
*
* Sends a broadcast to the media scanner to tell it about the new video.
*
* This is optional, but nice to have.
*/
static status_t notifyMediaScanner(const char* fileName)
// 果然有这样一个函数,然后在 main 函数的末尾调用了此函数:
if (err == NO_ERROR) {
    // Try to notify the media scanner. Not fatal if this fails.
    notifyMediaScanner(fileName);
}

那么,如果注释掉 notifyMediaScanner(fileName); 这一行,重新编译出来的 screenrecord 可执行程序在录屏时就不会发广播了,是不是就不用 root 权限了呢?
经过测试,是可以的。

修改源码,重新编译framework(其实我是编译整个源码,单独编译 framework 并未生成 screenrecord 可执行文件),然后替换掉system/bin/下的 screenrecord(这个操作是需要 root 权限的,所以本文仅仅是为了研究,并不能达到免 root 使用screenrecord 录屏。当然自己做 ROM 的话可以直接把改过的 screenrecord 打包进去),然后确实可以不用root权限执行。但是录屏结果是空文件(大小为0 kb),debug 信息如下:

Time limit reached
Encoder stopping; recorded 0 frames in 3 seconds
Stopping encoder and muxer

继续查看 screenrecord.cpp ,取消
#define LOG_NDEBUG 0
这一行的注释,即打开 ALOGV 的开关,重新编译,替换手机中的 screenrecord ,app 再次调用 screenrecord 命令,日志如下:

C:\Users\wy>adb logcat | findstr /I "ScreenRecord"
10-17 10:36:17.435 9839 9839 V ScreenRecord: Creating codec
10-17 10:36:17.531 9839 9839 V ScreenRecord: Creating encoder input surface
10-17 10:36:17.533 9839 9839 V ScreenRecord: Starting codec
10-17 10:36:17.618 9839 9839 V ScreenRecord: Codec prepared
10-17 10:36:17.623 9839 9839 V ScreenRecord: Calling dequeueOutputBuffer
10-17 10:36:17.873 9839 9839 V ScreenRecord: dequeueOutputBuffer returned -11
10-17 10:36:17.873 9839 9839 V ScreenRecord: Got -EAGAIN, looping
10-17 10:36:17.873 9839 9839 V ScreenRecord: Calling dequeueOutputBuffer
10-17 10:36:18.124 9839 9839 V ScreenRecord: dequeueOutputBuffer returned -11
10-17 10:36:18.124 9839 9839 V ScreenRecord: Got -EAGAIN, looping
10-17 10:36:18.124 9839 9839 V ScreenRecord: Calling dequeueOutputBuffer

日志显示,在输出 buffer 的时候一直返回错误,不停的重复尝试,直到结束也没成功录屏一帧。

这就不知道什么原因了,还得去看代码。

先丢这里,望明白的大神指点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android实现录屏,您可以使用 MediaProjection API。下面是一个简单的示例代码: 首先,在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> ``` 然后,在您的 Activity 中,您可以按照以下步骤进行操作: 1. 请求录屏权限: ```java private static final int REQUEST_CODE = 1; private MediaProjectionManager mediaProjectionManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data); startRecording(mediaProjection); } } ``` 2. 开始录屏: ```java private MediaRecorder mediaRecorder; private VirtualDisplay virtualDisplay; private void startRecording(MediaProjection mediaProjection) { mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); mediaRecorder.setVideoEncodingBitRate(512 * 1000); mediaRecorder.setVideoFrameRate(30); // 保存录屏文件 File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "screen.mp4"); mediaRecorder.setOutputFile(file.getAbsolutePath()); try { mediaRecorder.prepare(); Surface surface = mediaRecorder.getSurface(); virtualDisplay = mediaProjection.createVirtualDisplay("ScreenRecording", getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels, getResources().getDisplayMetrics().densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, surface, null, null); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } ``` 3. 停止录屏: ```java private void stopRecording() { if (mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.reset(); mediaRecorder.release(); mediaRecorder = null; } if (virtualDisplay != null) { virtualDisplay.release(); virtualDisplay = null; } } ``` 请注意,这只是一个简单的示例代码,您可能需要根据您的需求进行适当的修改和完善。此外,录屏期间可能会有一些性能和兼容性方面的问题,您可能需要进行一些额外的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值