下面列出了怎么用android.media.projection.MediaProjection的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_MEDIA_PROJECTION:
if (resultCode == RESULT_OK && data != null) {
MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
if (mediaProjection == null) {
Log.e("@@", "media projection is null");
return;
}
MyApplication.setMediaProjection(mediaProjection);
onResultCode();
buttonstart.setText(R.string.app_but_stop);
buttonstart.setEnabled(true);
}else {
buttonstart.setText(R.string.app_but_shar);
buttonstart.setEnabled(true);
}
break;
}
}
/**
*
* @param st 发送线程
* @param mp MediaProjection
* @param width 视频宽度 1080
* @param height 视频高度 1920
* @param videoBitrate 视频 比特率 16777216
* @param videoFrameRate 视频 帧率 24
*/
public VideoSender(SocketServerThread st, MediaProjection mp,
int width,int height,
int videoBitrate,int videoFrameRate
){
socketServerThread=st;
this.width=width;
this.height=height;
this.videoBitrate=videoBitrate;
this.videoFrameRate=videoFrameRate;
this.mediaReader =new MediaReader(width,height,videoBitrate,
videoFrameRate,this,mp);
mediaReader.startEncode();
}
/**
*
*/
void initImageRead(MediaProjection mediaProjection) {
if (mediaProjection == null) {
LogHelper.e(TAG, "mediaProjection == null");
return;
}
int width = UIUtils.getWidthPixels();
int height = UIUtils.getRealHeightPixels();
int dpi = UIUtils.getDensityDpi();
mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
/**
* 获取getSurface
*/
mediaProjection.createVirtualDisplay("ScreenCapture",
width, height, dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
createNotificationChannel();
//Android Q 存在兼容性问题
MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) intent.getParcelableExtra("data"));
ColorPickManager.getInstance().setMediaProjection(mediaProjection);
if (ColorPickManager.getInstance().getColorPickerDokitView() != null) {
ColorPickManager.getInstance().getColorPickerDokitView().onScreenServiceReady();
}
} catch (Exception e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION) {
// NOTE: Should pass this result data into a Service to run ScreenRecorder.
// The following codes are merely exemplary.
MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
if (mediaProjection == null) {
Log.e("@@", "media projection is null");
return;
}
mMediaProjection = mediaProjection;
mMediaProjection.registerCallback(mProjectionCallback, new Handler());
startCapturing(mediaProjection);
}
}
private void startCapturing(MediaProjection mediaProjection) {
VideoEncodeConfig video = createVideoConfig();
AudioEncodeConfig audio = createAudioConfig(); // audio can be null
if (video == null) {
toast(getString(R.string.create_screenRecorder_failure));
return;
}
File dir = getSavingDir();
if (!dir.exists() && !dir.mkdirs()) {
cancelRecorder();
return;
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.US);
final File file = new File(dir, "Screenshots-" + format.format(new Date())
+ "-" + video.width + "x" + video.height + ".mp4");
Log.d("@@", "Create recorder with :" + video + " \n " + audio + "\n " + file);
mRecorder = newRecorder(mediaProjection, video, audio, file);
if (hasPermissions()) {
startRecorder();
} else {
cancelRecorder();
}
}
private VirtualDisplay getOrCreateVirtualDisplay(MediaProjection mediaProjection, VideoEncodeConfig config) {
if (mVirtualDisplay == null) {
mVirtualDisplay = mediaProjection.createVirtualDisplay("ScreenRecorder-display0",
config.width, config.height, 1 /*dpi*/,
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
null /*surface*/, null, null);
} else {
// resize if size not matched
Point size = new Point();
mVirtualDisplay.getDisplay().getSize(size);
if (size.x != config.width || size.y != config.height) {
mVirtualDisplay.resize(config.width, config.height, 1);
}
}
return mVirtualDisplay;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startScreenRecord(final Intent intent) {
if (DEBUG) Log.v(TAG, "Start screen recorder");
final int resultCode = intent.getIntExtra(EXTRA_RESULT_CODE, 0);
// get MediaProjection
final MediaProjection projection = mMediaProjectionManager.getMediaProjection(resultCode, intent);
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
if (projection != null && window != null) {
_mediaProjection = projection;
final DisplayMetrics metrics = new DisplayMetrics();
window.getDefaultDisplay().getRealMetrics(metrics);
final int density = metrics.densityDpi;
HyperionGrabberOptions options = new HyperionGrabberOptions(mHorizontalLEDCount,
mVerticalLEDCount, mFrameRate, mSendAverageColor);
if (DEBUG) Log.v(TAG, "Starting the recorder");
mHyperionEncoder = new HyperionScreenEncoder(mHyperionThread.getReceiver(),
projection, metrics.widthPixels, metrics.heightPixels,
density, options);
mHyperionEncoder.sendStatus();
}
}
public MediaReader(int width, int height,int videoBitrate, int videoFrameRate,
EncoderListener encoderListener, MediaProjection mp) {
super(width, height, videoBitrate, videoFrameRate, encoderListener);
mMediaProjection = mp;
this.mWidth=width;
this.mHeight=height;
this.videoBitrate=videoBitrate;
this.videoFrameRate=videoFrameRate;
initVirtualDisplay();
}
private ScreenRecorder createRecorder(MediaProjection mediaProjection, final VideoEncodeConfig video
, final File output) {
ScreenRecorder r = new ScreenRecorder(video,
1, mediaProjection, output.getAbsolutePath());
r.setCallback(new ScreenRecorder.Callback() {
long startTime = 0;
@Override
public void onStop(Throwable error) {
isRecording = false;
}
@Override
public void onStart() {
lastRecorderStartTime = System.currentTimeMillis();
LogUtil.e("yuawen", "录屏开始时间:" + lastRecorderStartTime);
mHandler.post(new Runnable() {
@Override
public void run() {
isRecording = true;
mNotifications.recording(0);
}
});
}
@Override
public void onRecording(long presentationTimeUs) {
if (startTime <= 0) {
startTime = presentationTimeUs;
}
long time = (presentationTimeUs - startTime) / 1000;
mNotifications.recording(time);
}
});
return r;
}
/**
* @param dpi for {@link VirtualDisplay}
*/
public ScreenRecorder(VideoEncodeConfig video, int dpi, MediaProjection mp,
String dstPath) {
mWidth = video.width;
mHeight = video.height;
mDpi = dpi;
mMediaProjection = mp;
mDstPath = dstPath;
mVideoEncoder = new VideoEncoder(video);
}
@TargetApi(21)
private VideoCapturer createScreenCapturer() {
if (mediaProjectionPermissionResultCode != Activity.RESULT_OK) {
return null;
}
return new ScreenCapturerAndroid(
mediaProjectionPermissionResultData, new MediaProjection.Callback() {
@Override
public void onStop() {
Log.e(TAG, "User revoked permission to capture the screen.");
}
});
}
/** @hide */
public VirtualDisplay createVirtualDisplay(@Nullable MediaProjection projection,
@NonNull String name, int width, int height, int densityDpi, @Nullable Surface surface,
int flags, @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler,
@Nullable String uniqueId) {
return mGlobal.createVirtualDisplay(mContext, projection,
name, width, height, densityDpi, surface, flags, callback, handler, uniqueId);
}
/**
* Get media projection media projection.
*
* @param context the context
* @return the media projection
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected static MediaProjection getMediaProjection(Context context){
MediaProjectionManager mgr= (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
if(mMediaProjection!=null){
mMediaProjection.stop();
mMediaProjection=null;
}
mMediaProjection = mgr.getMediaProjection(Activity.RESULT_OK, (Intent) screenshotPermission.clone());
return mMediaProjection;
}
public ScreenRecordThread(int width, int height, int bitrate, int dpi, MediaProjection mp, String dstPath) {
super(TAG);
mWidth = width;
mHeight = height;
mBitRate = bitrate;
mDpi = dpi;
mMediaProjection = mp;
mDstPath = dstPath;
}
private ScreenRecorder newRecorder(MediaProjection mediaProjection, VideoEncodeConfig video,
AudioEncodeConfig audio, File output) {
final VirtualDisplay display = getOrCreateVirtualDisplay(mediaProjection, video);
ScreenRecorder r = new ScreenRecorder(video, audio, display, output.getAbsolutePath());
r.setCallback(new ScreenRecorder.Callback() {
long startTime = 0;
@Override
public void onStop(Throwable error) {
runOnUiThread(() -> stopRecorder());
if (error != null) {
toast("Recorder error ! See logcat for more details");
error.printStackTrace();
output.delete();
} else {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
.addCategory(Intent.CATEGORY_DEFAULT)
.setData(Uri.fromFile(output));
sendBroadcast(intent);
}
}
@Override
public void onStart() {
// mNotifications.recording(0);
}
@Override
public void onRecording(long presentationTimeUs) {
if (startTime <= 0) {
startTime = presentationTimeUs;
}
long time = (presentationTimeUs - startTime) / 1000;
// mNotifications.recording(time);
}
});
return r;
}
public ScreenCapturer(Context context, MediaProjection mediaProjection, String savePath) {
sMediaProjection = mediaProjection;
mContext = context;
isScreenCaptureStarted = false;
new Thread() {
@Override
public void run() {
Looper.prepare();
mHandler = new Handler();
Looper.loop();
}
}.start();
if (TextUtils.isEmpty(savePath)) {
File externalFilesDir = mContext.getExternalFilesDir(null);
Log.d("WOW", "externalFilesDir:" + externalFilesDir.getAbsolutePath());
if (externalFilesDir != null) {
STORE_DIR = externalFilesDir.getAbsolutePath() + "/myScreenshots";
} else {
Toast.makeText(mContext, "No save path assigned!", Toast.LENGTH_SHORT);
}
} else {
STORE_DIR = savePath;
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
HyperionScreenEncoder(final HyperionThread.HyperionThreadListener listener,
final MediaProjection projection, final int width, final int height,
final int density, HyperionGrabberOptions options) {
super(listener, projection, width, height, density, options);
try {
prepare();
} catch (MediaCodec.CodecException e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public OwtScreenCapturer(Intent data, int width, int height) {
super(data, new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
}
});
this.width = width;
this.height = height;
}
ScreenCapturer(MediaProjection projection, int width, int height, int density) {
mHandler = new Handler();
mProjection = projection;
mWidth = width;
mHeight = height;
mDensity = density;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_PICK_APPLICATION && resultCode == RESULT_OK) {
startActivity(data);
} else if (requestCode == Constants.REQUEST_MEDIA_PROJECTION) {
boolean allowCapture = resultCode == RESULT_OK;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
if (prefs.getBoolean(Constants.PREF_CAPTURE_SCREEN_ENABLED, false) != allowCapture) {
SharedPreferences.Editor editor1 = prefs.edit();
editor1.putBoolean(Constants.PREF_CAPTURE_SCREEN_ENABLED, allowCapture);
editor1.apply();
}
if (resultCode == RESULT_OK) {
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
MediaProjection projection = projectionManager.getMediaProjection(RESULT_OK, data);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
if (mCapturer == null) {
mCapturer = new ScreenCapturer(projection, size.x, size.y, metrics.densityDpi);
}
}
} else if (requestCode == Constants.REQUEST_VALIDATE) {
if (resultCode == Activity.RESULT_CANCELED) {
UiUtil.showButtonDialog(this, null,
getString(R.string.prefsDisabledMissingPermissions),
android.R.string.ok, (dialogInterface, i) -> onStart(), -1, null);
} else {
onStart();
}
}
}
AbstractScreenCast(Context context, MediaProjection mediaProjection, int width, int height) {
mContext = context;
mMediaProjection = mediaProjection;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mDisplayDensityDpi = metrics.densityDpi;
mWidth = width;
mHeight = height;
}
ImageScreenCast(final Context context,
final MediaProjection mediaProjection,
final ImageReader imageReader,
int width, int height) {
super(context, mediaProjection, width, height);
mImageReader = imageReader;
}
/**
* MediaProjection の後始末を行います.
*/
synchronized void clean() {
MediaProjection projection = mMediaProjection;
if (projection != null) {
projection.stop();
mMediaProjection = null;
}
}
public ScreenCast(Context context, MediaProjection mediaProjection, VideoQuality videoQuality) {
mContext = context;
mMediaProjection = mediaProjection;
mVideoQuality = videoQuality;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mDisplayDensityDpi = metrics.densityDpi;
}
private void requestMediaProjection() throws IOException {
CountDownLatch latch = new CountDownLatch(1);
Intent intent = new Intent();
intent.setClass(mContext, MediaProjectionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("callback", new ResultReceiver(mCallbackHandler) {
@Override
protected void onReceiveResult(final int resultCode, final Bundle resultData) {
if (resultCode == Activity.RESULT_OK) {
Intent data = resultData.getParcelable("result");
if (data != null) {
mMediaProjection = mMediaProjectionMgr.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(new MediaProjection.Callback() {
@Override
public void onStop() {
clean();
}
}, new Handler(Looper.getMainLooper()));
}
}
latch.countDown();
}
});
mContext.startActivity(intent);
try {
latch.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new IOException();
}
}
public MediaScreenEncoder(final MediaMuxerWrapper muxer, final MediaEncoderListener listener,
final MediaProjection projection, final int width, final int height, final int density,
final int _bitrate, final int _fps) {
super(muxer, listener, width, height);
mMediaProjection = projection;
mDensity = density;
fps = (_fps > 0 && _fps <= 30) ? _fps : FRAME_RATE;
bitrate = (_bitrate > 0) ? _bitrate : calcBitRate(_fps);
final HandlerThread thread = new HandlerThread(TAG);
thread.start();
mHandler = new Handler(thread.getLooper());
}
public static void setMediaProjection(MediaProjection mediaProjection){
MyApplication.mediaProjection=mediaProjection;
}
public static MediaProjection getMediaProjection() throws Exception{
if (mediaProjection==null){
throw new Exception("MediaProjection Not Set!");
}
return mediaProjection;
}
private ScreenRecorder createRecorder(MediaProjection mediaProjection, final VideoEncodeConfig video
, final File output) {
ScreenRecorder r = new ScreenRecorder(video,
1, mediaProjection, output.getAbsolutePath());
r.setCallback(new ScreenRecorder.Callback() {
long startTime = 0;
@Override
public void onStop(Throwable error) {
mHandler.post(new Runnable() {
@Override
public void run() {
isRecording = false;
isCalculating = true;
recordBtn.setText(R.string.record__calculating);
LauncherApplication.getInstance().showToast(getString(R.string.record__please_wait));
}
});
BackgroundExecutor.execute(new Runnable() {
@Override
public void run() {
VideoAnalyzer.getInstance().doAnalyze(lastCalculateT1,video.exceptDiff
, lastVideoPath, new VideoAnalyzer.AnalyzeListener() {
@Override
public void onAnalyzeFinished(final long result) {
mHandler.post(new Runnable() {
@Override
public void run() {
isCalculating = false;
recordBtn.setText(R.string.record__start_record);
if (result <= 0) {
LauncherApplication.getInstance().showToast(getString(R.string.record__operation_fast));
} else {
addResultValue(result);
}
}
});
}
@Override
public void onAnalyzeFailed(final String msg) {
mHandler.post(new Runnable() {
@Override
public void run() {
isCalculating = false;
recordBtn.setText(R.string.record__start_record);
LauncherApplication.getInstance().showToast(msg);
}
});
}
});
}
}, 2000);
if (error != null) {
LogUtil.e(TAG, "stop record is error now... error msg:\n"
+ MiscUtil.stackTraceToString(error.getStackTrace()));
output.delete();
}
}
@Override
public void onStart() {
lastRecorderStartTime = System.currentTimeMillis();
LogUtil.w("yuawen", "录屏开始时间:" + lastRecorderStartTime);
mHandler.post(new Runnable() {
@Override
public void run() {
isRecording = true;
hasClicked = false;
recordBtn.setText(R.string.record__stop_record);
mNotifications.recording(0);
}
});
}
@Override
public void onRecording(long presentationTimeUs) {
if (startTime <= 0) {
startTime = presentationTimeUs;
}
long time = (presentationTimeUs - startTime) / 1000;
mNotifications.recording(time);
}
});
return r;
}