How can I develop a "mock" media player using javascript?

Let me explain my setup.

Currently, I have two HTML5 audio elements, which I'll refer to as Master and Slave.

Both of these elements load the same media file, but the Slave has its audio muted by default.

Each player can control the other (pause, play, scrub, etc).

They both have custom styling and receive duration data from an external JSON file instead of relying on audio.duration.

While everything functions perfectly, the downside is that the audio file is loaded twice for each song due to server-side authentication complexities.

Is there a way to mimic an audio element without actually using it for the muted player? Perhaps with a setTimeout function or something similar?

I just need to simulate the audio.currentTime value to update the progress bar, but I'm unsure where to begin. Any advice would be appreciated. Thank you!

EDIT - To clarify, my website operates similarly to Soundcloud, with multiple players (slaves) on screen that can be controlled individually or by the master footer player. All content, except for the Master player, is dynamically loaded via Ajax.

Answer №1

To imitate the slave audio player, you can create a visual representation as follows:

  • Construct the element using HTML & CSS and position it dynamically to allow for JavaScript animations. Initially, set the progress bar of the simulated player with this CSS:

    #progress-bar{
        left: 0px;
    }
    

    You can animate the fake player's progress bar like this, utilizing setInterval:

    function moveRight() {
      var progressBar = document.getElementById('progressBar');
      var pos = 0;
      //current position on X Axis
    
      let move = setInterval(frame,5);
      //begin moving, every 5 milliseconds
    
      function frame() {
          if (pos == 400) {
            clearInterval(move);
          } else {
            pos++; 
            progressBar.style.left = 5 + pos/10 + '%'; 
          }
      }
    }
    

You will need to adjust the values in this example according to your player's duration. Additionally, consider the length of the actual audio file when determining this line: if (pos == 400) Upon activating your master player, invoke the moveRight() function to simulate the movement of the faux slider. Experiment with the numerical parameters until the motion replicates that of a genuine audio player slider.

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

The issue I'm facing is that the ng-click functionality in Angular JS is not functioning properly when the

Upon loading the page, my JavaScript function creates a tree structure from JSON data and generates a list of anchor tags which look like this: <a ng-click="shareParent(4619)">Some data</a> Despite associating the ng-click directive with the ...

Displaying multiple strings on a single line using AngularJS

I'm attempting to show a multi-line string on a webpage using AngularJS. Here's what I have: <textarea ng-model="StringValue"></textarea> {{StringValue}} When you enter text into the textarea: "This is a string" it displa ...

I am curious as to why jQuery offers the done() method for promises, while the JavaScript promises documented by Mozilla do not. Is there a way to incorporate a done() method in vanilla JavaScript promises if needed?

What sets apart the Promises in Mozilla's JavaScript documentation (view API page) from those in jQuery's Promises (see API page)? It appears that Mozilla's promise only includes 2 methods: then and catch. On the other hand, jQuery's p ...

Chat application using Node.js without the need for Socket.IO

Recently delving into the world of Node.js, I stumbled upon the fs.watchFile() method. It got me thinking - could a chat website be effectively constructed using this method (along with fs.writeFile()) in comparison to Socket.IO? While Socket.IO is reliabl ...

In order to locate a matching element within an array in a JSON file and update it, you can use Node

Good day, I have a script that updates the value in a JSON file const fsp = require('fs').promises; async function modifyNumberInFile() { try { let data = await fsp.readFile('example.json'); let obj = JSON.parse(dat ...

Unable to shrink array within an object

I'm encountering an issue while trying to reduce an array within an object. The error message I receive is: push is not a function To begin, I initialized my arrays as empty and created an add function to use as the first argument: function add(a,b ...

Creating a specialized validator for an object within express-validation

Concern The custom validator is not providing any response. I need to validate the specified object using express-validator. The 'privilege' key must be valid and present in my array. Although the existence of the privilege object is optional, i ...

Using React Bootstrap to conditionally render columns within an array map

Within my React application, I am currently utilizing the map function to generate Bootstrap columns in the JSX code of the render method. One specific attribute within the array object I'm mapping is named "taken." Depending on whether this attribute ...

React Native package identifies a primary module for handling HTTPS requests

For my latest project, I decided to experiment with incorporating HTTPS. I began by creating a new project using expo init test and then installed the HTTPS library with npm install https. Next, I made edits to App.js by adding the following line at the t ...

What is the best way to retrieve multiple JSON files from a single directory using React?

I am currently working on a React project which includes a folder titled "data" within the src directory. Inside this data folder, there are multiple JSON files that I need to fetch. The issue is that I do not know the names of these JSON files. How can ...

Guide to selecting elements by applying a filter to their child attributes

My current DOM structure looks like this: <g class="box" rel="1"> <text editable="false">some text</text> </g> <g class="box" rel="2"> <text editable="true">some text</text> </g> <g class="box" re ...

When invoking an external API within a Firebase Cloud Function, it may result in a

I've been attempting to use a basic API for currency conversion within a Firebase Cloud Function written in Typescript, but I keep getting a 'null' response. import { https } from 'firebase-functions'; import * as axios from ' ...

Fill your HTML form effortlessly using data from Google Sheets

I am relatively new to this topic, but I'm seeking a solution to populate an Apps Script Web App HTML dropdown form with names directly from a Google Spreadsheet. At the moment, I've managed to retrieve an array of names from column A in my sprea ...

Enhancing the shadow effects on ThreeJS using buffergeometry

I'm currently working on a project where I have loaded a .gltf model in ThreeJS with a keyframe animation that alters the shape of the object (it's a point level animation). Everything seems to be functioning correctly, except for the fact that t ...

Getting a string from JSON using Javascript

I am trying to retrieve a specific skill from a variable: const skillsData = [ { name: "React", value: 390 }, { name: "CSS", value: 1200 }, { name: "JavsScript", value: 540 }, { name: "HTML", value: 1900 }, ...

When a user clicks on buttons other than the submit button, the input field redirects to another page

A helpful user named Vittore assisted me in displaying the first 5 list items of the unordered list and implementing page navigation with next and previous buttons flawlessly. The issue arises with an input field, which functions correctly by navigating ...

What steps should be taken in PHP to display a popup when the user input is found to be invalid?

I've been working on implementing a popup in PHP to show if the user enters an existing username, as per our teacher's requirement to use popUp instead of alert. I have set the visibility property of the popup to "Hidden" in CSS; <div class=&q ...

React Native - The size of the placeholder dictates the height of a multiline input box

Issue: I am facing a problem with my text input. The placeholder can hold a maximum of 2000 characters, but when the user starts typing, the height of the text input does not automatically shrink back down. It seems like the height of the multiline text ...

Using ng-repeat on a select element is replacing the original content

I'm struggling with the following code snippet: <select> <div class="arrow-up"></div> <option data-ng-repeat="x in y">{{x}}</option> </select> the resulting output doesn't show the arrow div like this ...

One-touch dial feature for mobile-friendly website

Currently working on a single page responsive website and looking for guidance on inserting a quick call button that will only be visible when accessed on mobile phones, while remaining hidden on desktops and tablets. Your assistance is greatly valued. Th ...