自己實現了Android的屏幕錄制App。
用了MediaProjection類來作為源,MediaRecoder來捕捉,編碼轉換為本地視頻。
效果圖:





主要是這段代碼開始錄像:
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);//開始錄像
錄像結束后在回調函數中處理:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != REQUEST_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;
}
//應用最小化 后台運行
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
mMediaProjectionCallback = new MediaProjectionCallback();//回調類
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);//注冊回調類
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
回調處理的事情其實就是將錄像的類全都關閉釋放:
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");
}
mMediaProjection = null;
stopScreenSharing();
}
}
createVirtualDisplay()是我們自己定義的
/*
參數說明:
* 第一個參數:虛擬畫面名稱
* 第二個參數:虛擬畫面的寬度
* 第三個參數:虛擬畫面的高度
* 第四個參數:虛擬畫面的標志
* 第五個參數:虛擬畫面輸出的Surface
* 第六個參數:虛擬畫面回調接口
其中最重要的就是第五個參數,錄制畫面輸出的地方,他這里介紹的是一個Surface類型,那么我們如果想操作錄制之后的視頻數據,就需要創建一個Surface類型即可。
1、如果想截屏,那么可以使用ImageReader類的getSurface方法獲取
2、如果想錄制視頻進行編碼,可以使用MediaRecorder類的getSurface方法獲取
*/
private VirtualDisplay createVirtualDisplay() { return mMediaProjection.createVirtualDisplay("MainActivity", DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, /*surface是mediaRecorder的,以mediaprojection為源輸出到surface,捕獲*/ mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/); }
弄完之后我們跳到另一個Activity,顯示錄制完的視頻的縮略圖,並添加分享播放和重新錄制功能。
顯示視頻縮略圖:
public Bitmap getVideoThumbnail(String filePath) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(filePath);
bitmap = retriever.getFrameAtTime();
}
catch(IllegalArgumentException e) {
e.printStackTrace();
}
catch (RuntimeException e) {
e.printStackTrace();
}
finally {
try {
retriever.release();
}
catch (RuntimeException e) {
e.printStackTrace();
}
}
return bitmap;
}
播放:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);//主要是這里指定了ACTION
File vedioFile = new File(Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS), "/video.mp4");
Uri uri = Uri.parse(vedioFile.getAbsolutePath());
intent.setDataAndType(uri,"video/*");
startActivity(intent);
分享:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("audio/*");
File vedioFile = new File(Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS), "/video.mp4");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vedioFile));
//設置分享列表的標題,並且每次都顯示分享列表
startActivity(Intent.createChooser(shareIntent, "分享到"));
