H5实现录屏功能代码

 复制下面的代码至html文件,然后直接打开html文件即可使用录屏功能

<!DOCTYPE html>
<html lang="zh-CN">
<head>
   <meta charset="utf-8">
   <title>屏幕录制</title>
   <style>
       body{
           font-family: Arial;
           margin: 4vh auto;
           width: 90vw;
           max-width: 600px;
           text-align: center;
       }
       #controls{
           text-align: center;
       }
       .btn{
           margin: 10px 5px;
           padding: 15px;
           background-color: #2bcbba;
           border: none;
           color: white;
           font-weight: bold;
           border-radius: 6px;
           outline: none;
           font-size: 1.2em;
           width: 120px;
           height: 50px;
       }
       .btn:hover{
           background-color: #26de81;
           cursor: hand;
       }
       .btn:disabled{
           background-color: #2bcbba80;
       }
       #stop{
           background-color: #fc5c65;
       }
       #video{
           margin-top: 10px;
           margin-bottom: 20px;
           border: 12px solid #a5adb0 ;
           border-radius: 15px;
           outline: none;
           width: 100%;
           height: 400px;
           background-color: black;
       }
       h1{
           color: #2bcbba;
           letter-spacing:-2.5px;
           line-height: 30px;
       }
       .created{
           color: lightgrey;
           letter-spacing: -0.7px;
           font-size: 1em;
           margin-top: 40px;
       }
       .created > a{
           color: #4b7bec;
           text-decoration: none;
       }
   </style>
</head>

<body>
<video autoplay='true' id='video' controls='true' controlsList='nodownload'></video>
<button class='btn' id='record' onclick='record()'>录制</button>
<button style='display: none' class='btn' id='stop' onclick='stop()'>停止</button>
<button disabled='true' class='btn' id='download' onclick='download()'>下载</button>
<div class='created'>  </div>
<footer>
    
</footer>
</body>
<script>
const video = document.getElementById('video')
const downloadBtn = document.getElementById('download')
const recordBtn = document.getElementById('record')
const stopBtn = document.getElementById('stop')
let recorder

async function record() {
  // 开始录屏
  let captureStream

  try{
    captureStream = await navigator.mediaDevices.getDisplayMedia({
      video: true,
      // audio: true,   not support
      cursor: 'always'
    })
  }catch(e){
    // 取消录屏或者报错
    alert("Could not get stream")
    return
  }

  downloadBtn.disabled = true
  recordBtn.style = 'display: none'
  stopBtn.style = 'display: inline'

  // 删除之前的 Blob
  window.URL.revokeObjectURL(video.src)

  video.autoplay = true

  // 实时的播放录屏
  video.srcObject = captureStream

  // new 一个媒体记录
  recorder = new MediaRecorder(captureStream)
  recorder.start()

  captureStream.getVideoTracks()[0].onended = () => {
    // 录屏结束完成
    recorder.stop()
  }

  recorder.addEventListener("dataavailable", event => {
    // 录屏结束,并且数据可用
    console.log("dataavailable------------")
    let videoUrl = URL.createObjectURL(event.data, {type: 'video/ogg'})

    video.srcObject = null
    video.src = videoUrl
    video.autoplay = false

    downloadBtn.disabled = false
    recordBtn.style = 'display: inline'
    stopBtn.style = 'display: none'
  })
}

function download(){
  // 下载
  const url = video.src
  const name = new Date().toISOString().slice(0, 19).replace('T',' ').replace(" ","_").replace(/:/g,"-")
  const a = document.createElement('a')

  a.style = 'display: none'
  // a.download = `${name}.ogg`
  a.download = `${name}.mp4`
  a.href = url

  document.body.appendChild(a)

  a.click()
}

function stop(){
  let tracks = video.srcObject.getTracks()
  tracks.forEach(track => track.stop())
  recorder.stop()
}
</script>
</html>

 效果:

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 `uniapp` 中,可以使用 `uni-app` 的 `uni-record` 插件来实现录屏功能。该插件支持在 iOS 和 Android 平台上录制屏幕或 App 内容,并可输出 MP4 格式的视频文件。具体实现步骤如下: 1. 安装 `uni-record` 插件 在 `uniapp` 项目的根目录下,打开命令行窗口,执行以下命令安装插件: ``` npm install uni-record --save ``` 2. 在需要使用录屏功能的 `vue` 文件中引入 `uni-record` 插件: ```javascript import uniRecord from '@dcloudio/uni-record'; ``` 3. 在需要录屏的函数中调用 `uniRecord` 的 `startRecord` 方法开始录屏: ```javascript uniRecord.startRecord({ success: res => { console.log('录屏成功', res); // res.videoPath 为录屏视频文件的本地路径 }, fail: err => { console.log('录屏失败', err); } }); ``` 4. 在录屏完成后,调用 `uniRecord` 的 `stopRecord` 方法停止录屏: ```javascript uniRecord.stopRecord(); ``` 注意事项: - 录屏期间,应保持 App 处于前台运行状态。 - 录屏过程中 App 可能会出现卡顿现象,需要针对性地优化 App 性能。 - iOS 平台上需要在 `Info.plist` 文件中添加以下权限声明: ```xml <key>NSMicrophoneUsageDescription</key> <string>需要您的同意,才能进行录屏</string> <key>NSAppleMusicUsageDescription</key> <string>需要您的同意,才能进行录屏</string> <key>NSCameraUsageDescription</key> <string>需要您的同意,才能进行录屏</string> <key>NSPhotoLibraryUsageDescription</key> <string>需要您的同意,才能进行录屏</string> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值