当前位置: 首页> 教育> 幼教 > Android 实现屏幕录制

Android 实现屏幕录制

时间:2025/7/10 12:49:39来源:https://blog.csdn.net/wolf0706/article/details/140801127 浏览次数:0次
  1. 添加权限和服务声明

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <serviceandroid:name=".ScreenService"android:enabled="true"android:exported="true"android:foregroundServiceType="mediaProjection"></service>
    
  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.IOExceptionclass ScreenService : Service() {private var mContext:Context?=nullprivate var projectionManager:MediaProjectionManager?=nullprivate var mMediaProjection:MediaProjection?=nulloverride fun onBind(intent: Intent): IBinder {TODO("Return the communication channel to the service.")}override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {mContext = thisvar resultCode = intent?.getIntExtra("resultCode", -1)var path = intent?.getStringExtra("path")var resultData: Intent? = intent?.getParcelableExtra("data")startNotification();projectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManagermMediaProjection = 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?=nullpendingIntent = 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 NotificationManagernotificationManager.createNotificationChannel(channel)startForeground(1, notification);}}private var isScreenRecoding = falseprivate var  mMediaRecorder: MediaRecorder?=nullprivate var mVirtualDisplay: VirtualDisplay? = nullprivate fun startRecording(filePath:String) {if (!isScreenRecoding){try {// 创建 MediaRecorder 并设置参数val metrics = DisplayMetrics()val windowManager: WindowManager = mContext?.getSystemService(WINDOW_SERVICE) as WindowManagerwindowManager.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)// 准备 MediaRecordermMediaRecorder?.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 = trueLog.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// 停止 VirtualDisplaymVirtualDisplay?.release()// 停止 MediaProjectionmMediaProjection?.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)}}
    }
    
关键字:Android 实现屏幕录制

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: