如何使用HTML在网站中显示相机输入

时间:2018-03-09 16:08:41

标签: html ios safari embed

我决定创建一个像TokBox这样的网站。但我遇到了一个问题。当我使用WebRTC时,我没有得到相机的输出,有人可以帮助我吗?另外,如果您需要更多信息,我正在使用Safari并允许我使用相机,但我无法看到输出。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>

<style>
#container {
    margin: 0px auto;
    width: 500px;
    height: 375px;
    border: 10px #333 solid;
}
#videoElement {
    width: 500px;
    height: 375px;
    background-color: #666;
}
</style>
</head>

<body>
<div id="container">
    <video autoplay="true" id="videoElement">

    </video>
</div>
<script>
var video = document.querySelector("#videoElement");

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;

if (navigator.getUserMedia) {       
    navigator.getUserMedia({video: true}, handleVideo, videoError);
}

function handleVideo(stream) {
    video.src = window.URL.createObjectURL(stream);
}

哦,在这里,我不知道在这里放错了什么。

function videoError(e) {
    // do something
}
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

好吧,你可以先为错误添加一个console.log。

Safari对自动播放视频有点挑剔,this sample添加了playinline属性。您还希望使用srcObject而不是URL.createObjectURL,并使用现代版本的API进行承诺。