This article will tell you how to manipulate THEOplayer (Web) to make an audio-only interface, using only CSS and JavaScript. This applies also to mobile SDKs, where CSS and javascript can be injected (see Resources).
to this
Web SDK | Android SDK | iOS SDK | tvOS SDK | Android TV SDK | Chromecast SDK |
---|---|---|---|---|---|
Yes | Unverified / Through CSS and JavaScript injection | Unverified / Through CSS and JavaScript injection | No | Unverified / Through CSS and JavaScript injection | N/A |
This section explains how to alter the default video player UI into the UI shown in the picture below, thus transforming a default THEOplayer instance in an audio player. This can, of course, be further customised to your liking to adapt to the rest of your implementation.
The alteration involves 8 steps:
In the following, you can find a description and a code example for each step.
To comfortably provide the required CSS during the next steps, we add CSS classes to two HTML elements, respectively:
function transformPlayer(forAudio) {
var player = document.querySelector('.vjs-fluid');
var controlbar = document.querySelector('.vjs-control-bar');
if (forAudio) {
player.classList.add('audioplayer');
controlbar.classList.add('audiocontrol');
} else {
player.classList.remove('audioplayer');
controlbar.classList.remove('audiocontrol');
}
}
There are a bunch of buttons in the control bar that might not be relevant for an audio-only stream. Hence, we hide the following buttons using CSS:
.audiocontrol .vjs-icon-subtitles,
.audiocontrol .theo-quality-label,
.audiocontrol .vjs-icon-audio,
.audiocontrol .vjs-icon-cog,
.audiocontrol .vjs-fullscreen-control,
.audiocontrol .theo-cast-button,
.audioplayer video {
display: none !important;
}
Now we need to make sure no content appears above our controls. Again, we use CSS to hide elements:
.audiocontrol .theoplayer-poster,
.audiocontrol .song-info,
.audioplayer video {
display: none !important;
}
Next up, decreasing the player height to 50px.
.audioplayer {
padding: 0 !important;
height: 50px !important;
}
By default, the control bar fades away to ensure better user experience for the video viewer. The following snippet ensures that the controls stay always visible.
.audiocontrol.vjs-control-bar {
visibility: visible !important;
opacity: 1 !important;
display: flex !important;
}
An optional step is to scale the loading icon that spins when the player stalls.
.audioplayer .vjs-loading-spinner {
transform: scale(0.3) !important;
}
We're almost there. We just need to remove the giant play-button that shows when the video hasn't started yet.
:not(.vjs-has-started).audioplayer .vjs-big-play-button {
display:none !important;
}
Should we stop here, the audio player would show 0:00 / 0:00 as time information, since the stream is not yet loaded. This looks a bit clumsy, so we can hide it until the stream has started.
:not(.vjs-has-started).audioplayer .vjs-current-time,
:not(.vjs-has-started).audioplayer .vjs-time-divider,
:not(.vjs-has-started).audioplayer .vjs-duration {
display: none !important;
}
.vjs-has-started .vjs-current-time,
.vjs-has-started .vjs-time-divider,
.vjs-has-started .vjs-duration {
display: flex !important;
}
Note: if you preload the source (see Resources), hiding the time information before the player starts playing may not be needed, as the duration will be already available.
Finally, we recommend you to set the audioOnly flag to true in the player configuration. In that case, the player will use an audio element instead of a video element for media playback. This is only supported for HLS streams for now. Otherwise, the player uses a video element instead. This plays without any issue on most platforms, however we have noticed that Firefox doesn’t handle video elements with height and width 0 well.
const player = new THEOplayer.Player(element, {
audioOnly: true,
});
You should end up with a player that looks like this:
You can further modify the audio player that you have at this point to better fit in your implementation.
The Web SDK code should be included in your Android (TV) project. The article at How to add CSS or JavaScript files to an Android/iOS projectexplains how you can add CSS and JavaScript files to your project.
The Web SDK code should be included in your iOS project. The article at How to add CSS or JavaScript files to an Android/iOS project explains how you can add CSS and JavaScript files to your project.
The demo below illustrates the transformation from video player to audio player.