How can I update the source of an HTML video element seamlessly without causing any glitches?

I'm attempting to create a video call interface that plays videos generated based on user responses. I want the ability to seamlessly switch between the currently displayed video and the next generated video without the HTML collapsing and rebuilding the video element.

I initially attempted to change the video source using JavaScript in hopes of achieving a smooth transition, but instead, the video would momentarily collapse before reappearing.

My current JavaScript code for this task looks like:

const videoElement = document.getElementById('video');
function changeSource(path){
        videoElement.src = path;
}

Answer №1

"I am looking for a way to seamlessly switch between the current video being displayed and the next generated video without causing the HTML to collapse and rebuild the video element."

The default behavior of the MPEG video decoder is to re-initialize when new data is encountered. Unfortunately, this cannot be avoided, so you will need to implement a workaround based on your coding expertise or video engineering knowledge.

(1) If all the video clips already exist and only need to be dynamically selected...

Easiest: Toggle between two video tags:

  • Utilize two <video> tags to display the "current video" and the "next video."
  • Control visibility using CSS styles (e.g., in Javascript eg:
    myvid.style.visibility = "hidden"
    ).
  • Utilize CSS properties: z-index, position, top, left, to position one video tag above the other.

Easier: Navigate to different clips within a single video file:

  • Combine (or concatenate) all videos into a single, continuous video file.
  • Loop through each clip based on time intervals (check current time via timeupdate event).
  • If transitioning to the next clip is required, seek to the corresponding time within the video and repeat the looping process.

(2) If the video clips are generated dynamically (e.g., lip sync for custom response's words)...

Harder: Utilize MediaSource Extensions to stream individual videos as fragments:

  • Convert/encode your videos into Fragmented MP4 format.
  • In MSE, start by sending the main header (initial MOOV atom).
  • Subsequently, feed the specific clip (as a fragment) that needs to be replayed.
  • To switch to a different video, input the bytes of the alternative video.

Using MSE to input video data does not prompt the <video> tag to reload.
The tag anticipates receiving video data periodically to remain active at all times.

An alternative to utilizing MSE involves using PHP to input video data to a <video> tag. This method entails using a .php file as the video source instead of a .mp4 file.

Both the MSE and PHP approaches necessitate understanding the MP4 file structure (comprising multiple byte values stored within an array, with bytes representing numbers ranging from 0 to 255). Knowledge of which bytes correspond to the file's header and audio/video frames is crucial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the depths of CSS table-cell and the impact of percentage-based

During my exploration of Github, I came across a fascinating menu display: If you pay attention, each item in the menu is assigned an equal width. In CSS, we typically give it a percentage value - but why? It's interesting to note that the parent div ...

Upon transitioning from typescript to javascript

I attempted to clarify my confusion about TypeScript, but I'm still struggling to explain it well. From my understanding, TypeScript is a strict syntactical superset of JavaScript that enhances our code by allowing us to use different types to define ...

Ways to extract information from a span element

I'm completely new to web scraping and I have encountered an issue where I need to extract data from span elements that do not have any specific classes assigned to them. Here's an example of how they appear: "<span content=\"3 ...

Show the HTML that is received as JSON when making an AJAX request

Previously, my AJAX call would return an array of data as JSON by using "echo json_encode(array)" in my PHP code. I would then loop through the result in my script to build HTML with the returned data before displaying it. Recently, I updated the code to ...

Vertical alignment of Bootstrap cards without responsiveness

I am facing an issue with two Bootstrap cards stacked vertically but not being responsive in height. When I place them inside a parent div with a fixed height, the lower card containing a table exceeds the set height. To better illustrate this problem, I h ...

On IOS, Three.js ensures that the RGB values of a texture are set to zero whenever the ALPHA value

Working on a WebGL project utilizing javascript and the three.js framework, I am in the process of crafting a custom shader with GLSL. In this shader, I must load various lookup tables to utilize individual RGBA values for calculations rather than renderin ...

"Receiving a 404 error when sending a POST request, but getting

When attempting to send a POST request, I encountered a 404 error response from the server. Strangely, when sending a GET request, I received a 200 response. I have experimented with different methods: $.ajax({ type:"POST", url: "script.php", ...

Exploring the world of unit testing for Sails JS 1.0 helper functions

Currently, I am in the process of setting up unit testing for my SailsJS 1.0 application. My goal is to simulate the DB without needing to execute 'sails lift' to run tests. In my application, there is a straightforward actions2 (node machine) h ...

Is it possible for me to bring in/need the Kik.js document instead of using the Kik Card?

Currently working on a mobile web app designed for Kik. Their documentation advises including the Kik.js file directly in your HTML like this: <!-- simply include this script in your webpage --> <script src="http://cdn.kik.com/kik/2.3.6/kik.js"&g ...

Flashing occurs while utilizing animation and scrolling

$('.mainNav a[href^="#"]').on('click', function (event) { var target = $(this.getAttribute('href')); if (target.length) { event.preventDefault(); $('body').stop(true).animate({ scrollTop: targe ...

Flashing Effect of Angular Ui-Bootstrap Collapse During Page Initialization

Below is the code snippet I've implemented to use the ui-bootstrap collapse directive in my Angular application. HTML: <div ng-controller="frequencyCtrl" style="margin-top:10px"> <button class="btn btn-default" ng-click="isCollapsed = ...

Evaluate the script based on the number of words, not the number of characters

I have encountered an issue where I am only able to count the characters of a content, but what I really need is the word length. Specifically, I want the CSS to take action when there are words of 20 characters, but not if those 20 characters consist of m ...

Is the order of SCSS (SASS) selectors important when dealing with nested classes?

Exploring SCSS Styles for List Items In this code snippet, I am investigating the order of selection for classes and pseudo-selectors in SCSS. Specifically, I am questioning whether &:before.active is equivalent to &.active:before. Here is an exa ...

When working with AngularJS, I noticed that the service function within a loop is only executed after all iterations have been completed

Controller Page $scope.page = { '1' : "small", '2' : "large", '3': "medium" }; $scope.form.appraisal_id = "abc123"; $scope.form.user_id = "efg123"; for(var prop in $scope.page ...

Extract script from webpage using Python and Beautiful Soup

In the process of scraping 5 different shopping websites, I've successfully gathered valuable data from StackOverflow and YouTube. However, I have encountered a roadblock with one particular website. After utilizing various methods such as AJAX, Googl ...

What is causing the PHP AJAX MySQL Chat Script to require a page refresh?

Seeking assistance with a chat script I developed using AJAX to insert data without page refresh. Although the data inserts successfully, I encounter the issue that a page refresh is required in order to view the newly inserted data. My implementation util ...

Tips for testing an API that relies on an external library such as "<script src="http://stripe[...]"

Currently, I am working on unit testing an API call to verify it has been executed with the correct properties. The API call is reliant on Stripe's external library that is connected to the window through index.html src="http://stripe[...]". However, ...

Transitioning from AngularJS version 1.5.0 to 1.5.8

My bower.json file contains various dependencies, including AngularJS at version 1.5.0. I am looking to update AngularJS to version 1.5.8 without causing issues for my team members. I attempted to use bower install angular#1.5.8 --save, but this resulted ...

Unable to Toggle Bootstrap 5 Dropdown

Take a look at my code below. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewpor ...

Utilizing Django for tables with over two items in a template

I've been on the lookout for a solution to create a Django template that can display more than just 2 values per row in a table. Imagine having a class with 5 attributes, and wanting these attribute values showcased in a table on your HTML page. Init ...