Skip to content

Audio Player

The Audio Player component creates a customizable audio player with controls for playback, volume adjustment, and track progress visualization.

See the Example by Serhii Pimenov on CodePen.

<!-- Basic usage -->
<audio data-role="audio-player" data-src="path/to/audio.mp3"></audio>
<!-- With custom options -->
<audio data-role="audio-player"
data-volume="0.8"
data-loop="true"
data-autoplay="true"
data-show-volume="true"
data-show-loop="true">
<source src="path/to/audio.mp3" type="audio/mp3">
<source src="path/to/audio.ogg" type="audio/ogg">
</audio>
<!-- With custom icons -->
<audio data-role="audio-player"
data-loop-icon="<span class='mif-loop2'></span>"
data-play-icon="<span class='mif-play'></span>"
data-stop-icon="<span class='mif-stop'></span>"
data-src="path/to/audio.mp3">
</audio>
ParameterTypeDefaultDescription
srcstring/arraynullAudio source URL or array of source objects with src and type properties
playlistarraynullPlaylist of audio tracks
volumenumber0.5Initial volume level (0-1)
loopbooleanfalseWhether to loop the audio
autoplaybooleanfalseWhether to start playing automatically
showLoopbooleantrueWhether to show the loop button
showPlaybooleantrueWhether to show the play/pause button
showStopbooleantrueWhether to show the stop button
showMutebooleantrueWhether to show the mute button
showFullbooleantrueWhether to show the fullscreen button
showStreambooleantrueWhether to show the progress bar
showVolumebooleantrueWhether to show the volume control
showInfobooleantrueWhether to show the time information
showPlaylistbooleantrueWhether to show playlist controls
showNextbooleantrueWhether to show the next track button
showPrevbooleantrueWhether to show the previous track button
showFirstbooleantrueWhether to show the first track button
showLastbooleantrueWhether to show the last track button
showForwardbooleantrueWhether to show the forward button
showBackwardbooleantrueWhether to show the backward button
showShufflebooleantrueWhether to show the shuffle button
showRandombooleantrueWhether to show the random button
loopIconstringIcon for the loop button
stopIconstringIcon for the stop button
playIconstringIcon for the play button
pauseIconstringIcon for the pause button
muteIconstring🔇Icon for the mute button
volumeLowIconstring🔈Icon for low volume
volumeMediumIconstring🔉Icon for medium volume
volumeHighIconstring🔊Icon for high volume
playlistIconstring📃Icon for the playlist button
nextIconstringIcon for the next track button
prevIconstringIcon for the previous track button
firstIconstringIcon for the first track button
lastIconstringIcon for the last track button
forwardIconstringIcon for the forward button
backwardIconstringIcon for the backward button
shuffleIconstring🔀Icon for the shuffle button
randomIconstring🎲Icon for the random button
onPlayfunctionMetro.noopCallback function triggered when audio starts playing
onPausefunctionMetro.noopCallback function triggered when audio is paused
onStopfunctionMetro.noopCallback function triggered when audio is stopped
onEndfunctionMetro.noopCallback function triggered when audio playback ends
onMetadatafunctionMetro.noopCallback function triggered when metadata is loaded
onTimefunctionMetro.noopCallback function triggered on time update
onAudioPlayerCreatefunctionMetro.noopCallback function triggered when the player is created

Plays the audio. If a source is provided, it will change the audio source before playing.

// Play current audio
Metro.getPlugin('#myAudio', 'audio-player').play();
// Play with new source
Metro.getPlugin('#myAudio', 'audio-player').play('path/to/new/audio.mp3');

Pauses the audio playback.

Metro.getPlugin('#myAudio', 'audio-player').pause();

Resumes playback if the audio is paused.

Metro.getPlugin('#myAudio', 'audio-player').resume();

Stops the audio playback and resets the position to the beginning.

Metro.getPlugin('#myAudio', 'audio-player').stop();

Sets the volume level. If no parameter is provided, returns the current volume.

// Set volume to 80%
Metro.getPlugin('#myAudio', 'audio-player').setVolume(0.8);
// Get current volume
const volume = Metro.getPlugin('#myAudio', 'audio-player').setVolume();

Toggles the loop mode.

Metro.getPlugin('#myAudio', 'audio-player').loop();

Toggles the mute state.

Metro.getPlugin('#myAudio', 'audio-player').mute();

Changes the audio source based on the data-src attribute.

// After updating the data-src attribute
Metro.getPlugin('#myAudio', 'audio-player').changeSource();

Changes the volume based on the data-volume attribute.

// After updating the data-volume attribute
Metro.getPlugin('#myAudio', 'audio-player').changeVolume();

Removes the audio player component and its associated elements.

Metro.getPlugin('#myAudio', 'audio-player').destroy();
EventDescription
onPlayTriggered when audio starts playing
onPauseTriggered when audio is paused
onStopTriggered when audio is stopped
onEndTriggered when audio playback ends
onMetadataTriggered when metadata is loaded
onTimeTriggered when time is updated
onAudioPlayerCreateTriggered when the player is created

The Audio Player component inherits styling from the Media Player component and can be styled using CSS variables.

VariableDefault (Light)Dark ModeDescription
--player-inactive-color#555555#aaaaaaColor of inactive controls
--player-active-color#00b500#60a917Color of active controls
--player-hover-color#ffffff#ffffffColor of controls on hover
--player-controls-backgroundrgba(34, 34, 34, 0.5)rgba(34, 34, 34, 0.5)Background color of the controls panel
/* Custom styling for a specific audio player */
#myCustomAudio {
--player-inactive-color: #888888;
--player-active-color: #2196F3;
--player-hover-color: #FFEB3B;
--player-controls-background: rgba(0, 0, 0, 0.7);
}
  • .audio-player - Main container class for the audio player
  • .media-player - Parent class inherited from the media player component
  • .controls - Container for player controls
  • .stream - Container for the progress bar
  • .volume - Container for the volume control
  • .info-box - Container for time information
<!-- Simple audio player -->
<audio data-role="audio-player" data-src="sounds/music.mp3"></audio>
<!-- Audio player with custom options -->
<audio data-role="audio-player"
data-volume="0.8"
data-loop="true"
data-show-info="true"
data-show-volume="true"
data-play-icon="<span class='mif-play'></span>"
data-pause-icon="<span class='mif-pause'></span>"
data-stop-icon="<span class='mif-stop'></span>"
data-src="sounds/music.mp3">
</audio>
// Play audio programmatically
Metro.getPlugin('#myAudio', 'audio-player').play();
// Set volume to 50%
Metro.getPlugin('#myAudio', 'audio-player').setVolume(0.5);
// Toggle mute
Metro.getPlugin('#myAudio', 'audio-player').mute();
  • The audio player component uses the HTML5 Audio API, so it supports audio formats that are compatible with the user’s browser.
  • Common supported formats include MP3, WAV, and OGG.
  • For better user experience, it’s recommended to use compressed audio formats to reduce loading times.
  • The component automatically displays a progress bar showing both playback position and buffering progress.
  • The volume control provides visual feedback for volume level changes.