前端录屏屏幕 Demo

代码实现了如下功能:录制-保存、录制-回放、直播(边录边回放)

<html>
  <head>
    <title>Capture</title>
    <meta charset="utf8">
  </head>
  <body>
    <button id="capture">Start</button>

    <video id="video"></video>
    <script>
      (function() {
        console.log(MediaRecorder.isTypeSupported("video/webm"))
        console.log(MediaRecorder.isTypeSupported("video/mp4"))
        console.log(MediaRecorder.isTypeSupported("video/mp4;codecs=avc1"))
      })()
    </script>
    <script>
      var RECORDING_ONGOING = false;
        var recordingToggle = document.getElementById("capture"); // The button

        recordingToggle.addEventListener("click", function () {
          RECORDING_ONGOING = !RECORDING_ONGOING; // Start / Stop recording
          if (RECORDING_ONGOING) {
            recordingToggle.innerHTML = "Stop";
            startRecording(); // Start the recording
          } else {
            recordingToggle.innerHTML = "Start";
            stopRecording(); // Stop screen recording
          }
        });
    </script>

    <script>
      var blob, mediaRecorder = null;
      var chunks = [];

      async function startRecording() {
        // 获取权限,会有弹窗让用户选择和确认
        stream = await navigator.mediaDevices.getDisplayMedia(
          { video: { mediaSource: "screen" }, audio: true }
        );

        deviceRecorder = new MediaRecorder(stream, { mimeType: "video/webm" });
        deviceRecorder.ondataavailable = (e) => {
          if (e.data.size > 0) {
            chunks.push(e.data);
          }
        }
        deviceRecorder.onstop = () => {
          chunks = [];
        }
        deviceRecorder.start(250)
      }

      function stopRecording() {
        var filename = window.prompt("File name", "video"); // Ask the file name

        deviceRecorder.stop(); // Stopping the recording
        blob = new Blob(chunks, { type: "video/webm" })
        chunks = [] // Resetting the data chunks
        var dataDownloadUrl = URL.createObjectURL(blob);

        // Downloadin it onto the user's device
        let a = document.createElement('a')
        a.href = dataDownloadUrl;
        a.download = `${filename}.webm`
        a.click()

        URL.revokeObjectURL(dataDownloadUrl)

        replay(blob)
      }

      // 回放录制内容
      function replay(blob) {
        const video = document.getElementById("video");
        video.src = window.URL.createObjectURL(blob);
        video.srcObject = null;
        video.controls = true;
        video.play();
      }

      // 直播
      function live() {
        const video = document.getElementById("video");
        video.srcObject = window.stream;
        video.controls = true;
        video.play();
      }
    </script>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,可以实现在Android 5.0及以上版本中使用MediaProjection API进行屏幕录制: ``` public class ScreenRecorder { private static final String TAG = "ScreenRecorder"; private MediaProjection mMediaProjection; private VirtualDisplay mVirtualDisplay; private MediaRecorder mMediaRecorder; private int mScreenWidth; private int mScreenHeight; private int mScreenDensity; private boolean mIsRecording; public ScreenRecorder(MediaProjection mediaProjection, int screenWidth, int screenHeight, int screenDensity) { mMediaProjection = mediaProjection; mScreenWidth = screenWidth; mScreenHeight = screenHeight; mScreenDensity = screenDensity; } public boolean isRecording() { return mIsRecording; } public void startRecording(String filePath) { mMediaRecorder = new MediaRecorder(); try { mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setVideoEncodingBitRate(512 * 1000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mScreenWidth, mScreenHeight); mMediaRecorder.setOutputFile(filePath); mMediaRecorder.prepare(); } catch (IOException e) { e.printStackTrace(); } mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG, mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null); mMediaRecorder.start(); mIsRecording = true; } public void stopRecording() { if (mMediaRecorder != null) { mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder = null; } if (mVirtualDisplay != null) { mVirtualDisplay.release(); mVirtualDisplay = null; } if (mMediaProjection != null) { mMediaProjection.stop(); mMediaProjection = null; } mIsRecording = false; } } ``` 使用该类进行屏幕录制的示例代码如下: ``` private MediaProjectionManager mMediaProjectionManager; private ScreenRecorder mScreenRecorder; private void startScreenRecord() { if (mMediaProjectionManager == null) { mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); } if (mScreenRecorder == null) { int screenWidth = getResources().getDisplayMetrics().widthPixels; int screenHeight = getResources().getDisplayMetrics().heightPixels; int screenDensity = getResources().getDisplayMetrics().densityDpi; mScreenRecorder = new ScreenRecorder(mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, mResultData), screenWidth, screenHeight, screenDensity); } if (!mScreenRecorder.isRecording()) { mScreenRecorder.startRecording(Environment.getExternalStorageDirectory() + "/test.mp4"); } } private void stopScreenRecord() { if (mScreenRecorder != null && mScreenRecorder.isRecording()) { mScreenRecorder.stopRecording(); } } ``` 需要注意的是,使用该示例代码进行屏幕录制需要先获取悬浮窗权限和录音权限。同时,还需要在AndroidManifest.xml文件中添加以下权限: ``` <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值