Creating a Dynamic Soundtrack: How to Embed an Audio

Success! I finally figured it out, with a little help from everyone.

I've been diving into the world of Audio Playlists in HTML and attempted to follow a tutorial on the topic found here: https://www.youtube.com/watch?v=vtZCMTtP-0Y. However, I encountered an issue where the playlist wouldn't play correctly. After a song finished, it would always revert back to the second song on the list. Additionally, I received a "Uncaught (in promise) DOMException" error in the console which left me stumped.

function audioPlayer() {
    var currentSong = 0;
    $("#audioPlayer")[0].src = $("playlist li a")[0];
    $("#audioPlayer")[0].play();
    $("#playlist li a").click(function(e) {
        e.preventDefault();
        $("#audioPlayer")[0].src = this;
        $("#audioPlayer")[0].play();
        $("#playlist li").removeClass("current-song");
        currentSong = $(this).parent().index();
        $(this).parent().addClass("current-song");
    });

    $("#audioPlayer")[0].addEventListener("ended", function() {
        currentSong++;
        if (currentSong == $("#playlist li a").length) {
            currentSong = 0;
        }
        $("#playlist li").removeClass("current-song");
        $("#playlist li:eq(" + currentSong + ")").addClass("current-song");
        $("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
        $("#audioPlayer")[0].play();
    });
}

In addition, I tried styling the currently playing song in blue using CSS. However, instead of just the active song being highlighted, all clicked songs turned blue.

#audioPlayer {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

#playlist li a {
  color: white;
  text-decoration: none;
}

#playlist .current-song a {
  color: #0b80f7;
}

If anyone has any insight or suggestions on how to resolve these issues, I would be extremely grateful for your assistance.

Success! I finally figured it out, with a little help from everyone.

Answer №1

It turns out the issue stemmed from incorrectly using the "a" element within a span tag like this:

<li class="current-song"> <span><a href="../music/lights.mp3"> Lights Down Low</a></span><span>3:43</span></li>

Instead, the correct approach is as follows:

<li class="current-song"> <a href="../music/lights.mp3"> Lights Down Low</a><span>3:43</span></li>

By rectifying this mistake in my script, everything started working smoothly again. It's moments like these that make me realize everyone has their off days. Grateful for the helpful comments!

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

Unusual AngularJS error encountered while performing calculations on object properties

After running a specific calculation in my code, I encountered an unexpected issue. if (typeof $scope.memoryTable[name][category]['total'] !== 'undefined') { $scope.memoryTable[name][category]['total'] = $scope.memoryTabl ...

Attempting to create a TypeScript + React component that can accept multiple types of props, but running into the issue where only the common prop is accessible

I am looking to create a component named Foo that can accept two different sets of props: Foo({a: 'a'}) Foo({a: 'a', b: 'b', c:'c'}) The prop {a: 'a'} is mandatory. These scenarios should be considered i ...

Unveiling the secrets of extracting element content using JavaScript (with JQuery) from an HTML object

I have written the code below to access the page "JQueryPage.aspx" and retrieve data from it using jQuery: <script type="text/javascript> $.get ( "JQueryPage.aspx", function(data) { alert("Data Loaded: " + data); ...

What could be causing this ajax function to fail?

I referred to a solution in response to this question: My objective is to execute a query on the database when a user clicks on a specific table row, enabling me to retrieve and display their information. Below is the piece of code I am using: AJAX: $( ...

Tips for formatting dates in Angular 6

I am currently working on a function that displays real-time dates based on user input. Currently, when the user enters the input, it is displayed in the front end as follows: 28.10.2018 10:09 However, I would like the date to change dynamically based on ...

Is there a way to use nightwatch.js to scan an entire page for broken images?

Hi there, I'm currently working on creating a test to ensure that all images are loaded on a webpage with just one single test. I assumed this would be a straightforward task that many people have already done before, but unfortunately, I haven' ...

Tips for smoothly fading out a preloader

I found this script on the internet and implemented it, but I am having trouble with the fadeout effect. I really want a seamless and smooth fadeout animation. var loader = document.getElementById('spinner'); window.addEventListener("load", ...

How can we best understand the concept of custom directives using the link method?

As a beginner in AngularJS, I am looking to implement an autocomplete feature for text input. My JSON data is stored in JavaScript and I need help simplifying the process. Can you provide me with a straightforward solution? The specific requirement is to ...

Locate the highest and lowest values within a .json file

When working on a graph using d3.js, one key step is setting up the scales for the axis. The data for the graph is stored in a Json file with multiple arrays, each representing different regions and years: [{ "id" : "AustraliaNewZealand" , "year" ...

PHP mail function does not properly align HTML table outputs

After fetching data from the database and inserting it into an HTML table, everything appears fine. However, upon pressing the send button to email the content, sometimes the alignment of the HTML table is disrupted in the email. It seems like specific col ...

Obtain the value of an element from a React UI Material component (e.g. ListItemText)

Incorporated an onClick() event where a function is invoked to retrieve and display the value of any clicked element within the HTML body in a web component. <div id="root" onclick="handleClick(event)"></div> This snippet o ...

The navbar toggler icon is not displaying

<header class="container"> <div class="container pt-3 col-md-5 col-sm-5 col-xs-6 text-center"> <a href="#" class="site-logo"><img src="#"></a> <button class="navbar-toggler" type="button" data-toggle="collapse" ...

The background image does not appear to be displaying correctly on GitHub pages

I've encountered an issue where my svgs are not displaying as background-image or background on GitHub pages, even though they work fine locally. I utilized sass as a preprocessor for css, in case that detail is helpful. If anyone has insights on how ...

Disappearances of sliding jQuery divs

I am currently working on a website using jQuery, and I have implemented a slide in and out div to display share buttons. The issue I am facing is that the code works perfectly on the first page, but on every other page, the div slides out momentarily and ...

Having trouble clicking a button using Python and Selenium that triggers the `openWindow` function

I am attempting to interact with a button using Python Selenium WebDriver (Chrome). Here is the HTML code of the button: <button type="button" class="button blue" onclick="openWindow(LINK_HERE, 'idpage6')">Like</button> (I had to re ...

Creating a .htaccess file for an AJAX page: A step-by-step guide

I'm currently working on a website that I plan to develop as a CMS. Everything is running smoothly in terms of page links and URLs, but I encounter a 404 error when making an AJAX request. Below are the contents of my .htaccess file and the AJAX reque ...

Ways to extract the first name and email address from a JSON payload

{ "userID": 1, "userHandle": "username", "first_name": "firstname", "last_name": "lname", "middle_initial": null, "email_address": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e203d250e29232f27 ...

Retrieve data using Ajax querying from a specific data source

Currently, I am attempting to construct a custom query using Ajax to interact with PHP/MySQL. Here is what I have so far: Javascript code: var i=2; fetchFromDBPHP("name", "tblperson", "id="+i); function fetchFromDBPHP(column, table, condition) { ...

What is the best way to align flexbox to the left?

I'm having trouble aligning the FileCard component to the start of the container. I attempted using flex-start in my styling, but the FileCards are still centered. This is the current code snippet: <div v-if="posts" ...

Using the same conditional statement on multiple pages within a Next.js application

I'm currently working on a project with Next.js and I've encountered an issue when fetching data from my server using the useSWR hook. There are certain conditions that need to be met in order to display specific content on the page, as shown in ...