javascript - 无法使用 Javascript 动态更改 HTML5 视频的来源

标签 javascript html video html5-video

一直在尝试更改 HTML5 视频的源视频(单击按钮后)。但是我遇到了错误。下面的代码和错误:
相关HTML:

<video id="video2" playsinline controls muted preload="auto" style="z-index: -1; position: absolute;">
    <source src="exercise_media/vid_exercise_sample_1.mp4" type="video/mp4"  > 
</video>
(关于 z-index 的东西 - 这个视频播放然后被一个重新绘制它的 Canvas 抓取,作为整个应用程序的一部分。可能不相关但我想我会提到它。)
相关的 Javascript。启动视频,播放良好。这有点深奥,因为我是从别人的样本开始的。
loadCompanionVideo();

export async function loadCompanionVideo() {
    setStatusText('Setting up companion video...');
  try {
    video2 = await loadVideo2();
  } catch (e) {
    let info = document.getElementById('info');
    info.textContent = '(video player) this device type is not supported yet, ' +
      'or this browser does not support video capture: ' + e.toString();
    info.style.display = 'block';
    throw e;
  }
}

async function loadVideo2() {
  const video2 = await setupVideo2();
  return video2;
}

async function setupVideo2() {
  
  const video2 = document.getElementById('video2');
  
  video2.width = videoWidth2;   // some external numbers
  video2.height = videoHeight2; // just to customize width and height; works fine
  
  return video2;
  
}
所以这很好,我的第一个视频exercise_media/vid_exercise_sample_1.mp4玩得很好。但是,我想将视频源更改为exercise_media/vid_exercise_sample_2.mp4 ,相同的格式,位于相同的文件夹中,等等。(我通过将它硬编码到上面的 HTML 中来确保这个视频很好,它也可以正常播放。)
这是我一直在尝试更改的内容:
function buttonFunctionChangeVideoSrc() {
    document.getElementById("video2").pause();
    //document.getElementById("#video2 > source").src = "exercise_media/vid_exercise_sample_2.mp4";
    document.getElementById("video2").src = "exercise_media/vid_exercise_sample_2.mp4";
    document.getElementById("video2").load();
    document.getElementById("video2").play();
    
}
无济于事,("#video2 > source").src也不是 ("video2").src工作。在第一种情况下,错误是:

Uncaught TypeError: Cannot set property 'src' of null at HTMLButtonElement The video pauses and stays frozen.


在第二种情况下,尝试 video2.src直接(与 document.getElementById("video2") 相同),错误是

Uncaught (in promise) DOMException: Failed to load because no supported source was found.


屏幕的视频部分变为白色/空白。
播放/暂停功能运行良好,所以我知道我的 video2 有有效引用。目的。但我似乎无法改变来源。而且我也知道其他源视频也可以正常工作,因为我可以将其硬编码到 HTML 中并毫无问题地播放。但我似乎无法在它们之间动态切换。
任何帮助深表感谢。

添加更多我的代码来看看。不用担心姿势的问题,我的应用程序可以分析视频,并且当我只有一个视频与网络摄像头并排时工作正常(这就是我的应用程序所做的)。
问题是我无法将 video2 源更改为不同的 MP4。事实上,我可以让它播放 MP4 的唯一方法是在 HTML 中明确设置它。
HTML:
<div id="canvases" class="canvas-container">
        <div id='main' style='display:none'>
            <video id="video" playsinline style=" -moz-transform: scaleX(-1);
            -o-transform: scaleX(-1);
            -webkit-transform: scaleX(-1);
            transform: scaleX(-1);
            display: none;
            ">
            </video>
            <canvas id="output" class="camera-canvas"></canvas>
            <canvas id="keypoints" class="camera-canvas"></canvas>
            
            <video id="video2" playsinline controls muted style="z-index: -1; position: absolute;" 
            >
            <source id="source2" src="exercise_media/vid_exercise_sample_1.mp4" type="video/mp4"  > 
<!-- hard-coding the src in source is the ONLY way I can play a video here at all...  --> 

            </video>
            
            <canvas id="output2" class="camera-canvas2"></canvas>
            <canvas id="keypoints2" class="camera-canvas2"></canvas>

        <canvas class="illustration-canvas"></cavnas>
        <canvas class="illustration-canvas2"></cavnas>

    </div>
Javascript:
export async function bindPage() {
  setupCanvas();
  setupCanvas2();
  buttonSetup();
  toggleLoadingUI(true);
  setStatusText('Loading AI models...');
  posenet = await posenet_module.load({
    architecture: defaultPoseNetArchitecture,
    outputStride: defaultStride,
    inputResolution: defaultInputResolution,
    multiplier: defaultMultiplier,
    quantBytes: defaultQuantBytes
  });
  setStatusText('Loading FaceMesh model...');
  facemesh = await facemesh_module.load();
  facemesh2 = await facemesh_module.load();
  setStatusText('Loading Avatar file...');
  let t0 = new Date();
  await parseSVG(Object.values(avatarSvgs)[0]);

  setStatusText('Setting up camera...');
  try {
    video = await loadVideo();
  } catch (e) {
    let info = document.getElementById('info');
    info.textContent = '(web cam) this device type is not supported yet, ' +
      'or this browser does not support video capture: ' + e.toString();
    info.style.display = 'block';
    throw e;
  }
  
  try {
    video2 = await loadVideo2();
  } catch (e) {
    console.log("Error loading companion video :: "+e);
  }
  console.log(video2); // shows the full html
  playpauseFunction(); // start video2
  
  toggleLoadingUI(false);
  
  detectPoseInRealTime(video, posenet); //actual application, works fine
  
}

async function loadVideo2() {
  const video2 = await setupVideo2();
  return video2;
}

async function setupVideo2() {
  
  const video2 = document.getElementById('video2');
  
  //video2.setAttribute("src", vid_url_1); //does not work 
  //document.getElementById("source2").src = vid_url_2; // does nothing
  
  videoWidth2orig = video2.videoWidth;
  videoHeight2orig = video2.videoHeight; // gives the actual e.g. 640 x 360
  //.. I do some stuff below to set video2 width/height, works fine
  
  return video2;
  
}

function playpauseFunction() {
    try {
        if (playpause == true) {
            video2.pause();
            playpause = false;
            //alert("Workout paused. Click Play/Pause to resume.");
        } else if (playpause == false) {
            video2.play();
            playpause = true;
        }   
    } catch (e) {
        console.log("playpauseFunction exception :: "+e);
    }
}
现在,就像我说的,如果我将 src 硬编码到 HTML 中的 <source>标签,即 <source id="source2" src="exercise_media/vid_exercise_sample_1.mp4" type="video/mp4" >该应用程序运行良好,如下所示:
enter image description here
(工作正常,网络摄像头在视频旁边实时播放,我的骨骼跟踪在播放时在右侧视频上运行。)
我想要做的是点击例如显然,“练习样本 2”并更改右侧的视频。我尝试过的任何方法都没有奏效,例如:
function setupExercise2() {
    
    video2.setAttribute('autoplay', 'true');

    document.getElementById("source2").src = "exercise_media/vid_exercise_sample_2.mp4";
    //video2.setAttribute("src", "exercise_media/vid_exercise_sample_2.mp4"); // have tried both, neither work
    video2.load();

    const playPromise = video2.play()  // trigger video load

    console.log(playPromise); 

    if ( playPromise !== undefined ) {
       playPromise.then(() => {
          // video should start playing as soon as it buffers enough
       }).catch(error => {
          // playback failed, log error
          console.log(error);
       })
    }

}
特别是对于第一行(未注释),控制台说:Promise {<pending>}__proto__: Promise[[PromiseState]]: "pending"[[PromiseResult]]: undefined右侧视频变为白色,没有播放任何内容。如果我尝试使用 video2.setAttribute未注释行,然后控制台记录:
Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "rejected"
[[PromiseResult]]: DOMException: Failed to load because no supported source was found.
code: 9
message: "Failed to load because no supported source was found."
name: "NotSupportedError"
__proto__: DOMException
需要明确的是,我可以在 vid_exercise_sample_{2,3,..}.mp4 中的任何一个中进行硬编码。进入 html 并且它们播放和运行都很好,所以我不认为这是问题所在。
希望我现在提供了一个非常完整的图片!

最佳答案

以下示例代码显示了一个视频 .src动态更新。
有两个按钮用于在不同来源之间切换。
它已在 Windows PC 上使用 Chrome、Edge 和 Firefox 浏览器进行了测试。

<!DOCTYPE html>
<html><head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> </head>

<body>

<video id="video2" width="600" height="480" controls style="z-index: 1; overflow:hidden; position: absolute; top: 10px; left: 10px">
<source type="video/mp4">
</video>

<canvas id="myCanvas" width="600" height="480" style="z-index: 1; overflow:hidden; position: absolute; top: 10px; left: 640px">
</canvas>

<div style="z-index: 1; overflow:hidden; position: absolute; top: 510px; left: 10px">
<button onclick="ChangeVideoSrc( 1 )">Play Video 1</button>
<button onclick="ChangeVideoSrc( 2 )">Play Video 2</button>
</div>

<script>

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var timer_DrawCanvas; //# updated later in code

//# Example video links that work on my testing...
vid_url_1 = "https://seed191.bitchute.com/pupNZR0eMW9M/4CU38o2lkgyj.mp4";
vid_url_2 = "https://seed171.bitchute.com/VBdNMrOHylJh/2BMyQPl6BSpZ.mp4";

//# Try your own links if the above works...
//vid_url_1 = "exercise_media/vid_exercise_sample_1.mp4"
//vid_url_2 = "exercise_media/vid_exercise_sample_2.mp4"

//# Set initial starting vid SRC (used on page load)
const myVid = document.getElementById("video2");
myVid.setAttribute("src", vid_url_1);

//# Setup event listeners
myVid.addEventListener("canplay", handle_Media_Events);
myVid.addEventListener("loadeddata", handle_Media_Events);
myVid.addEventListener("play", handle_Media_Events);
myVid.addEventListener("pause", handle_Media_Events);
myVid.addEventListener("ended", handle_Media_Events);

function drawVideo() 
{
    ctx.drawImage(myVid, 0, 0, 600, 480);
}

function ChangeVideoSrc( vidNum ) 
{
    myVid.src = window[ "vid_url_" + vidNum ];
    myVid.load();
}

function handle_Media_Events()
{
    //# if enough data to begin playback
    if ( event.type == "canplay" ) { myVid.play(); }
    
    //# if first frame is available
    if ( event.type == "loadeddata" ) 
    { ctx.drawImage( myVid, 0, 0, 600, 480 ); } //# draws re-sized
    
    if ( event.type == "play" )
    { timer_DrawCanvas = setInterval( drawVideo, 30 ); }
    
    if ( (event.type == "pause") || (event.type == "ended") )
    { clearInterval( timer_DrawCanvas ); }
    
}

</script>

</body>
</html>
希望这将为您实现“使用 Javascript 动态更改 HTML5 视频的源”这一任务提供引用点。探索还在继续……
当你有机会测试代码时问任何问题。

关于javascript - 无法使用 Javascript 动态更改 HTML5 视频的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66539029/

相关文章:

video - 如何使用 FFMPEG 将 Y4M 视频文件拆分为多个 Y4M 文件?

css - HTML5 视频背景颜色与网站背景颜色不匹配——在某些浏览器中,有时

video - 如何查找视频 (.AVI .MP4) 的实际录制日期?

javascript - 如何检查用户是否可以看到并单击某个元素?

html - Web 开发——学习 Web 开发语言、平台——Grails 或 Ruby 或任何其他语言?

javascript - 动态生成内容的固定侧边栏的滚动

javascript - 如何检索 JavaScript 生成的 Html

html - 如何在链接或表单中指定 DELETE 方法?

Javascript:如何在动态确定的变量上调用方法?

javascript - 循环遍历字母表并将 ID 添加到 A 标签