最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't setting video element srcObject doesn't work inside Vue.js App - Stack Overflow

programmeradmin1浏览0评论

Consider the following app (code simplified)

<div id="app">
<video id="my-vid" autoplay></video>
</div>

JS

I am setting document.getElementById("my-vid").objectSrc = stream media stream (after asking the user for the usual user media, etc...) in plain javascript (that is without any vue js property binding).

It doesn't work (browser not displaying media stream, but a console.log still shows a valid MediaStream object.

When i move the video element outside the VueJS app however, it works !

<div id="app">
</div>
<video id="my-vid" autoplay></video>

What could be the reason for that?

Consider the following app (code simplified)

<div id="app">
<video id="my-vid" autoplay></video>
</div>

JS

I am setting document.getElementById("my-vid").objectSrc = stream media stream (after asking the user for the usual user media, etc...) in plain javascript (that is without any vue js property binding).

It doesn't work (browser not displaying media stream, but a console.log still shows a valid MediaStream object.

When i move the video element outside the VueJS app however, it works !

<div id="app">
</div>
<video id="my-vid" autoplay></video>

What could be the reason for that?

Share Improve this question edited Feb 20, 2019 at 15:50 Romain Bruckert asked Feb 20, 2019 at 14:57 Romain BruckertRomain Bruckert 2,61032 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I guess the correct way is

document.getElementById("video").srcObject = stream

I used it this way and it works pretty well..

<template>
  <div class="container">
    <video id="videotag" autoplay></video>
  </div>
</template>

<script>
export default {
  name: "video",
  ponents: {},
  mounted() {
     navigator.getUserMedia = navigator.getUserMedia ||
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia;

        // Start video camera
        navigator.getUserMedia({
                video: true,
                audio: false
            },
            function (stream) {
              document.getElementById('videotag').srcObject = stream
            },
            function (error) {
              console.error(error)
            }
        )
  }
};
</script>

In vue, you should never use document.getElementById(). Anything you can do with document.getElementById() you can do with ref. Try...

<div id="app">
   <video ref="myVid" autoplay></video>
</div>

then in your script, inside Vue...

var me = this;
// Start video camera
navigator.getUserMedia({
        video: true,
        audio: false
    },
    function (stream) {
        me.$refs.myVid.srcObject = stream
    },
    function (error) {
        console.error(error)
    }
)

This is because DOM level id is scoped to the whole page (whole app). From inside your little ponent, you can't dictate what ids other ponents might use, and conflicts are possible. Ref only needs to be unique within your ponent.

发布评论

评论列表(0)

  1. 暂无评论