[英]How to record screen and take screenshots, using Android API?
Android在Kitkat和Lollipop上獲得了新的API,用於視頻捕獲屏幕。 您可以通過ADB工具或通過代碼(從Lollipop開始)來完成。
自新API發布以來,許多應用程序都使用此功能,允許記錄屏幕,微軟甚至制作了自己的Google-Now-On-tap競爭對手應用程序。
使用ADB,您可以使用:
adb shell screenrecord /sdcard/video.mp4
您甚至可以在Android Studio內部執行此操作。
我找不到任何有關如何使用API的教程或解釋,這在代碼中是有意義的。
我找到的唯一的地方是文件( 這里 ,在“屏幕截圖和分享”下),告訴我這個:
Android 5.0允許您使用新的android.media.projection API為您的應用添加屏幕捕獲和屏幕共享功能。 例如,如果要在視頻會議應用中啟用屏幕共享,此功能很有用。
新的
createVirtualDisplay()
方法允許您的應用程序將主屏幕的內容(默認顯示)捕獲到Surface對象中,然后您的應用可以通過網絡發送該對象。 API僅允許捕獲非安全屏幕內容,而不是系統音頻。 要開始屏幕捕獲,您的應用必須首先通過使用通過createScreenCaptureIntent()
方法獲得的Intent啟動屏幕捕獲對話框來請求用戶的許可。有關如何使用新API的示例,請參閱示例項目中的MediaProjectionDemo類。
事實是,我找不到任何“MediaProjectionDemo”樣本。 相反,我發現了“屏幕截圖”樣本,但我不明白它是如何工作的,因為當我運行它時,我所看到的只是一個閃爍的屏幕,我不認為它將視頻保存到一份文件。 樣本看起來非常錯。
如何使用新API執行這些操作:
另外,我如何自定義它(分辨率,請求的fps,顏色,時間......)?
Ken White正確建議並且您可能已經涵蓋的第一步是正式提供的示例代碼 。
我之前使用過他們的API。 我同意截圖很簡單。 但是,屏幕錄制也是類似的。
我將在3個部分回答您的問題,並將鏈接包起來。 :)
1.開始錄像
private void startScreenRecord(final Intent intent) {
if (DEBUG) Log.v(TAG, "startScreenRecord:sMuxer=" + sMuxer);
synchronized(sSync) {
if (sMuxer == null) {
final int resultCode = intent.getIntExtra(EXTRA_RESULT_CODE, 0);
// get MediaProjection
final MediaProjection projection = mMediaProjectionManager.getMediaProjection(resultCode, intent);
if (projection != null) {
final DisplayMetrics metrics = getResources().getDisplayMetrics();
final int density = metrics.densityDpi;
if (DEBUG) Log.v(TAG, "startRecording:");
try {
sMuxer = new MediaMuxerWrapper(".mp4"); // if you record audio only, ".m4a" is also OK.
if (true) {
// for screen capturing
new MediaScreenEncoder(sMuxer, mMediaEncoderListener,
projection, metrics.widthPixels, metrics.heightPixels, density);
}
if (true) {
// for audio capturing
new MediaAudioEncoder(sMuxer, mMediaEncoderListener);
}
sMuxer.prepare();
sMuxer.startRecording();
} catch (final IOException e) {
Log.e(TAG, "startScreenRecord:", e);
}
}
}
}
}
2.停止錄像
private void stopScreenRecord() {
if (DEBUG) Log.v(TAG, "stopScreenRecord:sMuxer=" + sMuxer);
synchronized(sSync) {
if (sMuxer != null) {
sMuxer.stopRecording();
sMuxer = null;
// you should not wait here
}
}
}
2.5。 暫停和恢復視頻錄制
private void pauseScreenRecord() {
synchronized(sSync) {
if (sMuxer != null) {
sMuxer.pauseRecording();
}
}
}
private void resumeScreenRecord() {
synchronized(sSync) {
if (sMuxer != null) {
sMuxer.resumeRecording();
}
}
}
希望代碼有所幫助。 這是我提到的代碼的原始鏈接 ,並且也從中派生出此實現(視頻錄制)。
3.截取屏幕而不是視頻
我認為默認情況下它很容易以位圖格式捕獲圖像。 您仍然可以繼續使用MediaProjectionDemo示例來捕獲屏幕截圖。
[編輯]:代碼加密截圖
一個。 根據設備寬度/高度創建虛擬顯示
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
灣 然后根據意圖或行動啟動屏幕捕獲 -
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
停止媒體投影 -
sMediaProjection.stop();
C。 然后轉換為圖像 -
//Process the media capture
image = mImageReader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
//Create bitmap
bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
//Write Bitmap to file in some path on the phone
fos = new FileOutputStream(STORE_DIRECTORY + "/myscreen_" + IMAGES_PRODUCED + ".png");
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
有幾種可用的Media Projection API實現(完整代碼) 。 一些其他鏈接可以幫助您進行開發 -
android-ScreenCapture -
根據android開發者的觀察結果
github
:)
screenrecorder - github
希望它有所幫助:)快樂的編碼和屏幕錄制!
PS:你能告訴我你正在談論的微軟應用嗎? 我沒用過它。 想嘗試一下:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.