Creating a video player with playlist functionality using HTML, Tailwind CSS, and JavaScript is an exciting project! Tailwind CSS provides an efficient way to style the components. Below is a simple example structure that includes a video player and a basic playlist functionality. We'll use HTML for structure, Tailwind CSS for styling, and JavaScript for interactivity.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Player with Playlist</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@3.2.1/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-white">
<div class="container mx-auto p-5">
<h1 class="text-3xl font-bold mb-5">Video Player with Playlist</h1>
<div class="flex flex-col md:flex-row">
<!-- Video Player -->
<div class="flex-grow mb-5 md:mb-0">
<video id="videoPlayer" class="w-full rounded shadow-lg" controls>
<source id="videoSource" src="" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<!-- Playlist -->
<div class="md:w-1/3 md:pl-5">
<h2 class="text-xl font-semibold mb-3">Playlist</h2>
<ul id="playlist" class="bg-gray-800 rounded shadow-lg overflow-y-auto h-64 p-3">
<!--
Example of dynamically added playlist item:
<li class="playlist-item cursor-pointer hover:bg-gray-700 p-2 rounded" data-url="path/to/video.mp4">Sample Video 1</li>
-->
</ul>
<div class="mt-3">
<button id="addToPlaylist" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Add Video to Playlist</button>
</div>
</div>
</div>
</div>
<script>
const videoPlayer = document.getElementById('videoPlayer');
const videoSource = document.getElementById('videoSource');
const playlist = document.getElementById('playlist');
const addToPlaylistBtn = document.getElementById('addToPlaylist');
const videos = [
{title: 'Sample Video 1', url: 'path/to/video1.mp4'},
{title: 'Sample Video 2', url: 'path/to/video2.mp4'},
{title: 'Sample Video 3', url: 'path/to/video3.mp4'}
];
// Load initial playlist
videos.forEach(video => addVideoToPlaylist(video.title, video.url));
// Function to add a video to the playlist
function addVideoToPlaylist(title, url) {
const li = document.createElement('li');
li.innerText = title;
li.setAttribute('data-url', url);
li.className = "playlist-item cursor-pointer hover:bg-gray-700 p-2 rounded";
li.onclick = () => {
videoSource.src = url;
videoPlayer.load();
videoPlayer.play();
};
playlist.appendChild(li);
}
// Add event listener to the button to add a new video
addToPlaylistBtn.addEventListener('click', () => {
const title = prompt('Enter video title:');
const url = prompt('Enter video URL:');
if (title && url) {
addVideoToPlaylist(title, url);
}
});
</script>
</body>
</html>
Key Points:
-
HTML & Tailwind CSS: The layout is structured using a flexbox to support a responsive design, easily accommodating both the video player and playlist on different screen sizes. Tailwind classes add styling such as padding, margins, colors, and rounded corners.
-
JavaScript:
- The script initializes a list of sample videos.
addVideoToPlaylist dynamically creates a playlist item reflecting a video's title and URL.
- Playlist items are interactive elements (list items) that change the video’s source in the player when clicked.
- The "Add Video to Playlist" button allows users to input a new video title and URL which gets added to the playlist.
This example is a starting point and can be further expanded to include more advanced features such as drag-and-drop playlist reordering, video metadata display, and more sophisticated UI components.