本文主要是介绍浏览器屏幕共享,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<video autoplay playsinline muted></video>
<button id="startButton">开始</button>
<div id="errorMsg" style="color:red;">这里显示错误信息</div>
<script>
// let voices=[] //用了存放预设的人物信息
// speechSynthesis.onvoiceschanged=function(){
// voices=speechSynthesis.getVoices().filter(i=>i.lang.includes('zh'))
// console.log(voices);
// let sayer=new SpeechSynthesisUtterance(`
// 我叫你好`)
// sayer.voice=voices[0]
// speechSynthesis.speak(sayer)
// }
const startButton=document.getElementById('startButton')
startButton.addEventListener('click',()=>{
navigator.mediaDevices.getDisplayMedia({video:true}).then(handleSuccess,handleError)
})
function handleSuccess(stream){
startButton.disabled=true
const video=document.querySelector('video')
video.srcObject=stream
stream.getVideoTracks()[0].addEventListener('ended',()=>{
errorMsg('The user has ended sharing the screen')
startButton.disabled=false
})
}
function handleError(error){
errorMsg(`getDisplayMedia error:${error.name}`,error)
}
function errorMsg(msg,error){
const errorElement=document.querySelector('#errorMsg')
errorElement.innerHTML+=`<p>${msg}</p>`
if(typeof error!=='undefined'){
console.error(error);
}
}
</script>
</body>
</html>
这篇关于浏览器屏幕共享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!