android realizes screen recording function

  • 2021-11-14 07:09:42
  • OfStack

In this paper, we share the specific code of android to realize the screen recording function for your reference. The specific contents are as follows

1. mian. activity


package com.fpt.screenvideo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.projection.MediaProjectionManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

 private static final String TAG = "MainActivity";

 private Button mTextView,off_btn;

 private static final String RECORD_STATUS = "record_status";
 private static final int REQUEST_CODE = 1000;

 private int mScreenWidth;
 private int mScreenHeight;
 private int mScreenDensity;

 /**  Is video recording turned on  */
 private boolean isStarted = false;
 /**  Whether it is standard definition video  */
 private boolean isVideoSd = true;
 /**  Do you want to turn on audio recording  */
 private boolean isAudio = true;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.i(TAG, "onCreate");
  if(savedInstanceState != null) {
   isStarted = savedInstanceState.getBoolean(RECORD_STATUS);
  }
  getView() ;
  getScreenBaseInfo();
 }
 private void getView() {
  mTextView = findViewById(R.id.button_control);
  off_btn=findViewById(R.id.button_contro2);
  off_btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
////    Intent service = new Intent(this, ScreenRecordService.class);
//    stopService(service);
//    isStarted = !isStarted;
   }
  });
  if(isStarted) {
   statusIsStarted();
  } else {
   statusIsStoped();
  }
  mTextView.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(isStarted) {

     stopScreenRecording();// Function 
     statusIsStoped();// It's just status 
     Log.i(TAG, "Stoped screen recording");
    } else {
     startScreenRecording();// Function 
    }
   }
  });

  RadioGroup radioGroup = (RadioGroup) findViewById(R.id.redio_group);
  radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    switch (checkedId) {
     case R.id.sd_button:
      isVideoSd = true;
      break;
     case R.id.hd_button:
      isVideoSd = false;
      break;

     default:
      break;
    }
   }
  });

  CheckBox audioBox = (CheckBox) findViewById(R.id.audio_check_box);
  audioBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    isAudio = isChecked;
   }
  });
 }

 /**
  *  When screen recording is turned on UI Status 
  */
 private void statusIsStarted() {
  mTextView.setText(" Stop recording ");
  mTextView.setBackgroundColor(Color.GREEN);
 }

 /**
  *  After finishing the screen recording UI Status 
  */
 private void statusIsStoped() {
  mTextView.setText(" Start recording ");
  mTextView.setBackgroundColor(Color.RED);
 }

 /**
  *  Get screen-related data 
  */
 private void getScreenBaseInfo() {
  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  mScreenWidth = metrics.widthPixels;
  mScreenHeight = metrics.heightPixels;
  mScreenDensity = metrics.densityDpi;
 }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);
  outState.putBoolean(RECORD_STATUS, isStarted);
 }

 /**
  *  Getting Permissions for Screen Recording 
  */
 private void startScreenRecording() {
  // TODO Auto-generated method stub
  MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
  Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
  startActivityForResult(permissionIntent, REQUEST_CODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  if(requestCode == REQUEST_CODE) {
   if(resultCode == RESULT_OK) {
    //  Gain permissions and start Service Start recording 
    Intent service = new Intent(this, ScreenRecordService.class);
    service.putExtra("code", resultCode);
    service.putExtra("data", data);
    service.putExtra("audio", isAudio);
    service.putExtra("width", mScreenWidth);
    service.putExtra("height", mScreenHeight);
    service.putExtra("density", mScreenDensity);
    service.putExtra("quality", isVideoSd);
    startService(service);
    //  Screen recording has started, modifying UI Status 
    isStarted = !isStarted;
    statusIsStarted();
//    simulateHome(); // this.finish(); //  You can close it directly Activity
    Log.i(TAG, "Started screen recording");
   } else {
    Toast.makeText(this, " Pop up the prompt box ", Toast.LENGTH_LONG).show();
    Log.i(TAG, "User cancelled");
   }
  }
 }

 /**
  *  Close screen recording, that is, stop recording Service
  */
 private void stopScreenRecording() {
  // TODO Auto-generated method stub
  Intent service = new Intent(this, ScreenRecordService.class);
  stopService(service);
  isStarted = !isStarted;
 }

 /**
  *  Simulation HOME Key to return to the desktop 
  */
 private void simulateHome() {
  Intent intent = new Intent(Intent.ACTION_MAIN);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.addCategory(Intent.CATEGORY_HOME);
  this.startActivity(intent);
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  //  Here will BACK Key simulates the HOME Key to return to desktop function (not necessary) 
  if(keyCode == KeyEvent.KEYCODE_BACK) {
   simulateHome();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }

}

2. ScreenRecordService


package com.fpt.screenvideo;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScreenRecordService extends Service {
 private static final String TAG = "ScreenRecordingService";

 private int mScreenWidth;
 private int mScreenHeight;
 private int mScreenDensity;
 private int mResultCode;
 private Intent mResultData;
 /**  Whether it is standard definition video  */
 private boolean isVideoSd;
 /**  Do you want to turn on audio recording  */
 private boolean isAudio;

 private MediaProjection mMediaProjection;
 private MediaRecorder mMediaRecorder;
 private VirtualDisplay mVirtualDisplay;

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
  Log.i(TAG, "Service onCreate() is called");
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  // TODO Auto-generated method stub
  Log.i(TAG, "Service onStartCommand() is called");

  mResultCode = intent.getIntExtra("code", -1);
  mResultData = intent.getParcelableExtra("data");
  mScreenWidth = intent.getIntExtra("width", 720);
  mScreenHeight = intent.getIntExtra("height", 1280);
  mScreenDensity = intent.getIntExtra("density", 1);
  isVideoSd = intent.getBooleanExtra("quality", true);
  isAudio = intent.getBooleanExtra("audio", true);

  mMediaProjection = createMediaProjection();
  mMediaRecorder = createMediaRecorder();
  mVirtualDisplay = createVirtualDisplay(); //  Must be in the mediaRecorder.prepare()  Called after, otherwise an error is reported "fail to get surface"
  mMediaRecorder.start();

  return Service.START_NOT_STICKY;
 }

 private MediaProjection createMediaProjection() {
  Log.i(TAG, "Create MediaProjection");
  return ((MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE)).getMediaProjection(mResultCode, mResultData);
 }

 private MediaRecorder createMediaRecorder() {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  Date curDate = new Date(System.currentTimeMillis());
  String curTime = formatter.format(curDate).replace(" ", "");
  String videoQuality = "HD";
  if(isVideoSd) videoQuality = "SD";

  Log.i(TAG, "Create MediaRecorder");
  MediaRecorder mediaRecorder = new MediaRecorder();
//  if(isAudio) mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
  mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  mediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/" + videoQuality + curTime + ".mp4");
  mediaRecorder.setVideoSize(mScreenWidth, mScreenHeight); //after setVideoSource(), setOutFormat()
  mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //after setOutputFormat()
//  if(isAudio) mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //after setOutputFormat()
  int bitRate;
  if(isVideoSd) {
   mediaRecorder.setVideoEncodingBitRate(mScreenWidth * mScreenHeight);
   mediaRecorder.setVideoFrameRate(30);
   bitRate = mScreenWidth * mScreenHeight / 1000;
  } else {
   mediaRecorder.setVideoEncodingBitRate(5 * mScreenWidth * mScreenHeight);
   mediaRecorder.setVideoFrameRate(60); //after setVideoSource(), setOutFormat()
   bitRate = 5 * mScreenWidth * mScreenHeight / 1000;
  }
  try {
   mediaRecorder.prepare();
  } catch (IllegalStateException | IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Log.i(TAG, "Audio: " + isAudio + ", SD video: " + isVideoSd + ", BitRate: " + bitRate + "kbps");

  return mediaRecorder;
 }

 private VirtualDisplay createVirtualDisplay() {
  Log.i(TAG, "Create VirtualDisplay");
  return mMediaProjection.createVirtualDisplay(TAG, mScreenWidth, mScreenHeight, mScreenDensity,
    DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null);
 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Log.i(TAG, "Service onDestroy");
  if(mVirtualDisplay != null) {
   mVirtualDisplay.release();
   mVirtualDisplay = null;
  }
  if(mMediaRecorder != null) {
   mMediaRecorder.setOnErrorListener(null);
   mMediaProjection.stop();
   mMediaRecorder.reset();
  }
  if(mMediaProjection != null) {
   mMediaProjection.stop();
   mMediaProjection = null;
  }
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
}

3. androidManifest. xml authority


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

4. Registration of service


<service android:name=".ScreenRecordService"/>

Related articles: