Android 实现屏幕录制
创始人
2024-11-14 11:04:36
0
  1. 添加权限和服务声明

      
  2. 创建屏幕录制的 Service

    import android.app.* import android.content.Context import android.content.Intent import android.graphics.BitmapFactory import android.hardware.display.DisplayManager import android.hardware.display.VirtualDisplay import android.media.CamcorderProfile import android.media.MediaRecorder import android.media.projection.MediaProjection import android.media.projection.MediaProjectionManager import android.os.Build import android.os.IBinder import android.util.DisplayMetrics import android.util.Log import android.view.WindowManager import androidx.core.app.NotificationCompat import java.io.IOException  class ScreenService : Service() {      private var mContext:Context?=null     private var projectionManager:MediaProjectionManager?=null     private var mMediaProjection:MediaProjection?=null     override fun onBind(intent: Intent): IBinder {         TODO("Return the communication channel to the service.")     }      override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {         mContext = this         var resultCode = intent?.getIntExtra("resultCode", -1)         var path = intent?.getStringExtra("path")         var resultData: Intent? = intent?.getParcelableExtra("data")         startNotification();         projectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager         mMediaProjection = resultCode?.let { resultData?.let { it1 -> projectionManager?.getMediaProjection(it, it1) } }         path?.let { startRecording(it) }         return super.onStartCommand(intent, flags, startId)     }      private var NOTIFICATION_CHANNEL_ID="id";     private var NOTIFICATION_CHANNEL_NAME="channel";     private var NOTIFICATION_CHANNEL_DESC="desc";     private fun startNotification() {         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {             var notificationIntent = Intent(mContext, ScreenService::class.java)             var pendingIntent: PendingIntent?=null             pendingIntent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {                 PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);             } else {                 PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);             }             var  notificationBuilder = mContext?.let {                 NotificationCompat.Builder(it, NOTIFICATION_CHANNEL_ID)                     .setLargeIcon(BitmapFactory.decodeResource(mContext!!.resources, R.drawable.ic_launcher_foreground))                     .setSmallIcon(R.drawable.ic_launcher_foreground)                     .setContentTitle("start record")                     .setContentText("=== start record ===")                     .setContentIntent(pendingIntent)             };             var  notification = notificationBuilder?.build();             var  channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);             channel.description = NOTIFICATION_CHANNEL_DESC;             var  notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager             notificationManager.createNotificationChannel(channel)             startForeground(1, notification);         }     }       private var isScreenRecoding = false     private var  mMediaRecorder: MediaRecorder?=null     private var mVirtualDisplay: VirtualDisplay? = null     private fun startRecording(filePath:String) {         if (!isScreenRecoding){             try {                 // 创建 MediaRecorder 并设置参数                 val metrics = DisplayMetrics()                 val windowManager: WindowManager = mContext?.getSystemService(WINDOW_SERVICE) as WindowManager                 windowManager.defaultDisplay.getMetrics(metrics)                 mMediaRecorder = MediaRecorder()                 mMediaRecorder?.setVideoSource(MediaRecorder.VideoSource.SURFACE)                 mMediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)                 mMediaRecorder?.setOutputFile(filePath)                 mMediaRecorder?.setVideoSize(metrics.widthPixels, metrics.heightPixels)                 mMediaRecorder?.setVideoEncoder(MediaRecorder.VideoEncoder.H264)                 mMediaRecorder?.setVideoEncodingBitRate(1920*1080 * 3)                 mMediaRecorder?.setVideoFrameRate(30)                  // 准备 MediaRecorder                 mMediaRecorder?.prepare()                  // 创建 VirtualDisplay 以获取屏幕内容                 mVirtualDisplay = mMediaProjection?.createVirtualDisplay(                     "ScreenRecord",                     metrics.widthPixels, metrics.heightPixels, metrics.densityDpi,                     DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,                     mMediaRecorder?.surface, null, null                 )                  // 开始录制                 mMediaRecorder?.start()                 isScreenRecoding = true                 Log.i(ScreenUtil.TAG,"开始录屏 $filePath")             } catch (e: IOException) {                 Log.e(ScreenUtil.TAG, "录屏失败: " + e.message)                 e.printStackTrace()             }         }     }     public fun stopRecording() {         if (isScreenRecoding) {             try {                 // 停止录制                 mMediaRecorder?.stop()                 mMediaRecorder?.reset()                 mMediaRecorder?.release()                 mMediaRecorder = null                  // 停止 VirtualDisplay                 mVirtualDisplay?.release()                  // 停止 MediaProjection                 mMediaProjection?.stop()                 Log.i(ScreenUtil.TAG,"结束录屏")             } catch (e: Exception) {                 Log.e(ScreenUtil.TAG, "停止录屏失败: " + e.message)                 e.printStackTrace()             }             isScreenRecoding = false         }     }      override fun onDestroy() {         stopRecording()         super.onDestroy()     } } 
  3. 启动和停止录制

    private var mProjectionManager: MediaProjectionManager? = null var screenService:Intent?=null fun start(){ 	mProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager 	// 请求录屏权限 	startActivityForResult(mProjectionManager?.createScreenCaptureIntent(), 500); } fun stop(){ 	stopService(screenService) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {     super.onActivityResult(requestCode, resultCode, data)     if (requestCode == 500){         screenService = Intent(mConext, ScreenService::class.java)         screenService?.let {             it.putExtra("resultCode", resultCode);             it.putExtra("data", data);             it.putExtra("path", "screen.mp4");             startForegroundService(it)         }     } } 

相关内容

热门资讯

微信炸金花房卡有没有购买/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
终于找到“微信炸金花链接在哪里... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信炸金花房卡链接在哪弄的/怎... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
正版授权“微信牛牛房卡客服微信... 九酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
微信玩链接炸金花房卡/战皇大厅... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
买房卡的链接炸金花房卡/微信斗... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“牛牛房卡哪里有卖的... 起点大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
微信群牛牛房卡怎么买/新九天大... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
房卡必备教程“微信牛牛房卡在哪... 新全游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
微信群链接拼三张房卡/新518... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
一分钟推荐“炸金花房卡链接怎么... 金牛座金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信里面炸金花房卡在哪买/新蓝... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“哪里有卖微信炸金花... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
在哪里买斗牛微信房卡/茄子娱乐... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
正版授权“牛牛房卡哪里有卖的”... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
炸金花房卡链接在哪弄的/牛牛房... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信炸金花房卡如何购买/悟空大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“炸金花房卡专卖店联... 新神盾是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享...
微信炸金花房间怎么弄/新皇豪大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
ia实测“微信金花群怎么买房卡... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...