代码迁移
This commit is contained in:
92
src/Global/ScreenRecord/index.js
Normal file
92
src/Global/ScreenRecord/index.js
Normal file
@ -0,0 +1,92 @@
|
||||
class ScreenRecord {
|
||||
constructor() {
|
||||
this.start()
|
||||
}
|
||||
|
||||
start() {
|
||||
navigator.mediaDevices.getDisplayMedia({
|
||||
video: true
|
||||
})
|
||||
.then((stream) => {
|
||||
// 需要更好的浏览器支持
|
||||
// const mime = MediaRecorder.isTypeSupported('video/webm; codecs=vp9')
|
||||
// ? 'video/webm; codecs=vp9'
|
||||
// : 'video/webm'
|
||||
this.mediaRecorder = new MediaRecorder(stream, {
|
||||
// mimeType: mime,
|
||||
mimeType: 'video/webm',
|
||||
})
|
||||
|
||||
let chunks = []
|
||||
this.mediaRecorder.addEventListener('dataavailable', function (e) {
|
||||
chunks.push(e.data)
|
||||
})
|
||||
|
||||
this.mediaRecorder.addEventListener('stop', async () => {
|
||||
try {
|
||||
let blob = new Blob(chunks, {
|
||||
type: 'video/mp4',
|
||||
})
|
||||
const opts = {
|
||||
suggestedName: '视频录制.mp4',
|
||||
types: [
|
||||
{
|
||||
description: '文件类型',
|
||||
accept: {
|
||||
'video/mp4': ['.mp4'],
|
||||
}
|
||||
}
|
||||
],
|
||||
excludeAcceptAllOption: true
|
||||
};
|
||||
|
||||
const handle = await window.showSaveFilePicker(opts); // 打开保存文件对话框
|
||||
const writable = await handle.createWritable(); // 创建可写入的文件对象
|
||||
// 写入视频内容
|
||||
writable.write(blob);
|
||||
await writable.close();
|
||||
YJ.Global.ScreenRecord.screenRecord = null
|
||||
} catch (error) {
|
||||
console.info('文件保存失败:', error);
|
||||
}
|
||||
|
||||
// let blob = new Blob(chunks, {
|
||||
// type: chunks[0].type,
|
||||
// })
|
||||
// let url = URL.createObjectURL(blob)
|
||||
|
||||
// let a = document.createElement('a')
|
||||
// a.href = url
|
||||
// a.download = 'video.webm'
|
||||
// a.click()
|
||||
// this.recording = false
|
||||
// YJ.Global.ScreenRecord.screenRecord = null
|
||||
})
|
||||
|
||||
// 必须手动启动
|
||||
this.mediaRecorder.start()
|
||||
})
|
||||
.catch((e) => {
|
||||
console.info('取消录屏')
|
||||
console.info(e)
|
||||
YJ.Global.ScreenRecord.screenRecord = null
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function startScreenRecord() {
|
||||
if (YJ.Global.ScreenRecord.screenRecord) {
|
||||
return '录屏任务进行中'
|
||||
} else {
|
||||
YJ.Global.ScreenRecord.screenRecord = new ScreenRecord()
|
||||
}
|
||||
}
|
||||
|
||||
function stopScreenRecord() {
|
||||
if (YJ.Global.ScreenRecord && YJ.Global.ScreenRecord.screenRecord && YJ.Global.ScreenRecord.screenRecord) {
|
||||
YJ.Global.ScreenRecord.screenRecord.mediaRecorder.stop()
|
||||
YJ.Global.ScreenRecord.screenRecord = null
|
||||
}
|
||||
}
|
||||
|
||||
export { startScreenRecord, stopScreenRecord }
|
Reference in New Issue
Block a user