If you're looking to create a customized set of audio controls for an HTML5 player, it's actually quite simple and can be done quickly using basic HTML, CSS, and some light Javascript event handling. This solution will give you a fully-functional custom audio player based on the design provided.
You can find the complete code and example in this jsFiddle: https://jsfiddle.net/mgaskill/zo3ede1c/. Feel free to explore and interact with it as it is a working audio player.
Overview of the Player
The player encompasses all the elements detailed in the original design. You can view this setup (and compare it to the original) in the first image linked below:
https://i.sstatic.net/cixuS.png
In addition to the core design, I added a collapsible "info tray" feature which can be toggled by clicking the "More Info" button on the right side. The second image demonstrates the deployed info tray:
https://i.sstatic.net/n0tT0.png
While there may be slight differences in colors and pixel specifics compared to the original design, the overall structure remains very close to the initial concept. Admittedly, my expertise lies more in functionality than styling, leaving room for enhancements particularly in the CSS aspect. Nonetheless, the player closely mirrors the original design in terms of layout, spirit, and usability.
Tools Used
To facilitate the development process, several external resources were leveraged. These include:
- jQuery: A personal preference over raw Javascript
- jQueryUI: Employed for track progress and volume control due to compatibility issues with certain browsers lacking HTML5 progress bar support
- FontAwesome: Utilized for play/pause and volume/mute button icons
- Noise Addicts Free MP3 Samples: Specifically, the captivating Semper Fidelis March
HTML Structure
The HTML approach involved treating each component within the audio controls panel as an independent element. The layout itself is fairly straightforward, with the most notable elements being the use of FontAwesome classes for the initial state symbols on the play/pause and volume/mute buttons.
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<audio id="player">
<source src="http://www.noiseaddicts.com/samples_1w72b820/2543.mp3" type="audio/mpeg" />
</audio>
<div id="audio-player">
<div id="controls">
<i id="play" class="fa fa-pause"></i>
<span id="start-time" class="time">00:00</span>
<div id="progressbar"></div>
<span id="time" class="time">00:00</span>
<i id="mute" class="fa fa-volume-up"></i>
<div id="volume"></div>
</div>
<div id="more-info-box">
<span id="more-info">MORE INFO</span>
</div>
<div id="info-tray">
Track: <span id="track">Semper Fidelis March</span>
</div>
</div>
All audio controls are encapsulated within the #audio-player
element.
CSS Styling
The CSS plays a significant role in bringing the HTML elements to life, providing color schemes, positioning directives, and interactive behaviors.
#audio-player {
height: 50px;
width: 500px;
overflow: hidden;
background-color: #2B2B2B;
color: white;
-webkit-user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-ms-user-select: none;
}
#controls {
height: 50px;
background-color: #808080;
width: 350px;
}
.time {
font-size: 10px;
color: white;
position: relative;
top: 14px;
margin: 5px;
}
.ui-progressbar {
background: #2B2B2B;
}
.ui-progressbar-value {
background: white;
}
#progressbar, #volume {
height: 10px;
display: inline-block;
border-radius: 0px;
border: none;
position: relative;
top: 16px;
}
#progressbar {
width: 150px;
}
#play, #mute {
font-size: 16px;
width: 20px;
position: relative;
top: 17px;
}
#play {
margin-left: 15px;
}
#volume {
width: 50px;
}
Although most styles are straightforward, certain aspects like text selection prevention and specific layouts require attention. For instance, browser-specific properties disable text selection:
-webkit-user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-ms-user-select: none;
Styling for jQueryUI progress bars utilizes pre-defined classes defining different appearance states:
.ui-progressbar {
background: #2B2B2B;
}
.ui-progressbar-value {
background: white;
}
Elements' display settings and positional adjustments are made through display: inline-block;
and position: relative;
rules.
Javascript Implementation
The Javascript portion mainly focuses on managing events related to various controls and statuses.
// JavaScript Code Block
The script contains event handlers for actions such as loading audio metadata, updating track progress during playback, handling user interactions with progress bars, managing play/pause and mute functions, and controlling the visibility of the information tray. Each function caters to a specific aspect of the audio player's behavior and responsiveness.