我尝试从下面的链接测试一个关于屏幕记录的示例代码,我修改了一些代码来禁用录制音频。
http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/
我在Android Studio V1.3中测试代码,但是我收到以下错误,文件capture.mp4为空。
我不确定是否必须在真正的手机中测试代码?你可以帮帮我吗 ?谢谢!
错误信息
09-22 06:41:50.250 2167-2167/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: screencapture.truiton.com.myapplication, PID: 2167
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at screencapture.truiton.com.myapplication.MainActivity.onToggleScreenShare(MainActivity.java:93)
at screencapture.truiton.com.myapplication.MainActivity$1.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
源代码
package screencapture.truiton.com.myapplication;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final int PERMISSION_CODE = 1;
private int mScreenDensity;
private MediaProjectionManager mProjectionManager;
private static final int DISPLAY_WIDTH = 480;
private static final int DISPLAY_HEIGHT = 640;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaProjectionCallback mMediaProjectionCallback;
private ToggleButton mToggleButton;
private MediaRecorder mMediaRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();
mProjectionManager = (MediaProjectionManager) getSystemService
(Context.MEDIA_PROJECTION_SERVICE);
mToggleButton = (ToggleButton) findViewById(R.id.toggle);
mToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToggleScreenShare(v);
}
});
mMediaProjectionCallback = new MediaProjectionCallback();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PERMISSION_CODE) {
Log.e(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this,
"Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
mToggleButton.setChecked(false);
return;
}
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
public void onToggleScreenShare(View view) {
if (((ToggleButton) view).isChecked()) {
shareScreen();
} else {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
stopScreenSharing();
initRecorder();
prepareRecorder();
}
}
private void shareScreen() {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
return;
}
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
//mMediaRecorder.release();
}
private VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
private class MediaProjectionCallback extends MediaProjection.Callback {
@Override
public void onStop() {
if (mToggleButton.isChecked()) {
mToggleButton.setChecked(false);
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
}
mMediaProjection = null;
stopScreenSharing();
Log.i(TAG, "MediaProjection Stopped");
}
}
private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
private void initRecorder() {
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/capture.mp4");
}
}
答案 0 :(得分:1)
根据我的经验,Android模拟器允许从曲面读取(这是录制中唯一真正需要的东西)但由于框架Java代码中的错误/限制而无法编码视频(有时会截取屏幕截图)。主要有两个原因:
模拟器可能不会声明支持EGL_ANDROID_recordable扩展,这是MediaRecorder / MediaCodec所要求的。您可以通过执行以下命令来检查扩展支持:
adb shell dumpsys SurfaceFlinger | grep EGL_ANDROID_recordable
请注意,上述限制并非专用于仿真器,也可能导致实际设备出现故障。您可以使用软件(在CPU上或通过GLES)进行格式转换,使用this answer中的方法大纲来解决这些问题,但无论如何,软件编码器的性能都会非常糟糕。