Creating a personalized music player using an audio tag with HTML, CSS, and JavaScript

I am currently working on developing a unique music player from scratch without utilizing the "controls" tag within the audio element. My goal is to build something akin to the SCM Music Player. I've chosen not to use the SCM-provided player due to its excessive space consumption on my website, difficulty in hiding/showing elements enclosed within a script tag, and negative impact on Y-Slow speed.

Visual representation of my envisioned design:

https://i.sstatic.net/l359S.jpg

Here is what I have accomplished so far: https://jsfiddle.net/e13gs8qg/6/ (Updated)

HTML: (Updated)

<audio id="player"  class="mediaplayerclass">
   <source title="Fruity Loops Own Track Copy" src="https://cdn.shopify.com/s/files/1/0804/2449/files/fruityloops_own_track_copy.mp3" />
   <source title="Fruity Loops Own Track Copy 2" src="https://cdn.shopify.com/s/files/1/0804/2449/files/fruityloops_own_track_copy_2.mp3" />
</audio>

<div class="playermenuwrapper">
    <button id="previoussong" class="previoussongclass">
        Previous
    </button>
    <button id="playpause" class="playpauseclass">
        play
    </button>
    <button id="nextsong" class="nextsongclass">
        Next
    </button>
    <div id="songtitle" class="titleclass"></div>
</div>

CSS:

.playermenuwrapper {
  text-align:center;
  margin: 0px auto;
  max-width:100%;
}
.previoussongclass {
  display:inline-block;
  width:80px;
}

.playpauseclass {
  display:inline-block;
  width:80px;
}

.nextsongclass {
  display:inline-block;
  width:80px;
}

.mediaplayerclass {
  display:block;
  width:150px;
  height:50px;
  margin: 0px auto;
}
.titleclass {
  display:inline-block;
  text-align:center;
  margin: 0px auto;
  width:250px;
}

JS: (Updated)

window.player = document.getElementById('player');
var playpause = document.getElementById('playpause');
var songtitle = document.getElementById('songtitle');
changesongtitle();
player.volume = 0.3;

playpause.onclick = function () {

    if (player.paused) {
        changesongtitle();
        player.play();
        playpause.innerHTML = 'pause';

    } else {
        player.pause();
        playpause.innerHTML = 'play';
        changesongtitle();
    }
}

function changesongtitle() {

    var songtitle = document.getElementById('songtitle');

    if(player.src = "https://cdn.shopify.com/s/files/1/0804/2449/files/fruityloops_own_track_copy.mp3") {
    songtitle.innerHTML = "Fruity Loops Own Track Copy";
    }

    if(player.src = "https://cdn.shopify.com/s/files/1/0804/2449/files/fruityloops_own_track_copy_2.mp3") {
    songtitle.innerHTML = "Fruity Loops Own Track Copy 2";
    }

}

I have extensively researched similar queries on stackoverflow but haven't found satisfactory solutions to my specific requirements.

  • How can I retrieve the title of the playing source file? (Updated)

  • Is it possible to implement functionality for the "left" and "right" buttons to switch between sources?

  • How can I integrate a volume slider into the player?

  • Is there a method to incorporate a timeline displaying the current duration of the played audio?

  • Lastly, how do I showcase both the current playback time and total duration of the audio track being played?

Guidance needed on leveraging the currentTime and duration properties for this purpose.

Answer №1

If you're looking to create a custom library tailored to your specific needs, I recommend checking out Mediaelement.js. It offers a wide range of features and is compatible with most browsers. Additionally, you can enhance this library by adding functions like changing songs based on user input since it's an open-source platform.

I have personally used Mediaelement.js and added custom features to it. You can check out my work here: https://github.com/hkasera/mediaelement-markers

Before diving into creating your custom library, it's important to have a good understanding of how HTML5 audio and video elements function. I recommend reading the following articles for more insight:

  1. http://www.html5rocks.com/en/tutorials/webaudio/intro/
  2. https://developer.mozilla.org/en-US/Apps/Build/Audio_and_video_delivery/Cross-browser_audio_basics

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

Obtaining the count of a specific column in Angular 6 by grouping objects based on the same value in an array

In TypeScript, I have an array of objects and I am using a foreach loop. The array contains 2 columns with a progress value of 100, while the rest have values less than 100. My goal is to calculate the count of columns with a progress value of 100, which ...

SQL queries can be used in Node.js to fetch data from various tables and display it on an HTML page

I have a problem with my expressjs app where I am trying to count records from multiple tables in a database and display the results on separate divs in HTML. Currently, the code is functional without any errors, but I am encountering an issue where the sa ...

Restrict the number of rows in a real-time search JSON data by utilizing the $.each method

Is there a way to limit the number of rows returned in live search JSON data through URL? I attempted to count the table rows and return false, but it did not work. Any suggestions on how to achieve this? $(document).ready(function() { $.ajaxSetup ...

Changing HTML elements dynamically within an ng-repeat using AngularJS directives

I have devised an angular directive where I execute an ng-repeat. The fundamental purpose of this directive is to interchange itself with a distinct directive that depends on a value forwarded into the original directive: <content-type-directive type=" ...

What could be causing my list of files to not properly append to FormData?

I have been working on this issue for the past three days. Despite trying various solutions from different sources, including seeking help from other websites, I am still unable to resolve it. My current challenge involves sending a post request with a Fo ...

Using JavaScript to consume ASP.net Script Services

My ASP.NET script service needs to be accessed from JavaScript using JSONP due to cross-domain restrictions. When dealing with a complex input structure, I have to construct the client-side input structure myself. How does the matching between the client ...

Can a simultaneous read/write conflict occur in JavaScript while browsing?

A situation has arisen where I am executing multiple (let's say four) AJAX calls using AngularJS http get, and I desire each call to invoke a callback function that increments a counter. This way, I can track when all four threads have finished. I am ...

There seems to be an issue with the CSV file, possibly indicating an error or the file may not be an SYLYK file when

After developing a node.js script to convert an array object into CSV format using the "objects-to-csv" library from NPM, I encountered an issue when opening the generated CSV file in WPS and Microsoft Office. The warning suggested that there was either an ...

The Django application is failing to interact with the AJAX autocomplete functionality

After typing the term "bi" into the search bar, I expected to see a username starting with those initials displayed in a dropdown list. However, nothing is showing up. Here are the codes I have used: search.html <html> <div class="ui-widget"> ...

What is the procedure for a parent component to transmit HTML to a child component within Angular?

How can a parent component pass multiple ng-templates to a child component? For example, the parent component includes multiple ng-templates: <app-childcomponent> <ng-template>Item A</ng-template> <ng-template>Item B</n ...

Selenium is unable to interact with hyperlink buttons in Python

I am currently working on a project where an automated program logs into an account and navigates through various webpages. My current struggle lies in the inability to click on any hyperlink buttons on a specific webpage to progress to the next page. I h ...

Tips for incorporating an HTML file using ng-include in double curly brace syntax {{ }}

Here is the code snippet I am working with: <div ng-repeat="pTabs in child.productTabs" ng-click="toggleProductTab($index)" ng-if="productTabIsActive(pTabs, $index)"> <div ng-repeat="specs in pTab.additionalSpecs"> <p>{{spec ...

Preventing an image from being repeated when using Canvas drawImage() without having to clear the entire canvas

How can I prevent multiple instances of the same image from smearing across the canvas when drawing it? The platforms seem to stick together and not separate properly. Why do I have to clear the entire rectangle for everything to disappear? Does anyone ha ...

Using JavaScript, conceal a specific Div by examining the content within another Div

I am attempting to implement some logic using JavaScript. The goal is to hide the "cart-button" div if the innerHTML value of a div with the class "b-format" is set to Audio, otherwise hide the "more-button" div. However, for some reason this functionality ...

In PHP, you can customize the colors to emphasize different data sets

In my MySQL database connected through PHP, I have data displayed in table format. My goal is to highlight certain data based on age with two conditions: Condition 1: - Color red (#FF7676) for ages greater than 24 Condition 2: - Color yellow (#F8FF00) fo ...

html form shifting positions based on screen resolution

I have been experimenting with a login screen design for my website recently. I created a form with a fixed position, but as the screen resolution changes, not only does the form's position shift, but also the image moves, causing an unwanted interse ...

ReactJS error: Unable to access the setState property

As a newcomer to ReactJS, I have been facing some challenges. I recently familiarized myself with the ES6 syntax, and it claims that the following pieces of code are equivalent in meaning. 1. YTSearch({key: API_KEY, term: 'nba'}, function(vide ...

What is the best way to eliminate the alert message "autoprefixer: Greetings, time traveler. We are now in the era of CSS without prefixes" in Angular 11?

I am currently working with Angular version 11 and I have encountered a warning message that states: Module Warning (from ./node_modules/postcss-loader/dist/cjs.js): Warning "autoprefixer: Greetings, time traveler. We are now in the era of prefix-le ...

Watch for changes in a nested collection in Angular using $scope.$watch

Within my Angular application, there is a checkbox list that is dynamically generated using nested ng-repeat loops. Here is an example of the code: <div ng-repeat="type in boundaryPartners"> <div class="row"> <div class="col-xs- ...

Encounter issue when using GAS withSuccessHandler function

I've developed a Google Sheets add-on that utilizes a modal dialog for the user interface. I encountered an issue with the success handler not running as expected, so I created a basic test interface to troubleshoot the problem. After the server-side ...