Creating a stylish video player website with playlist management and video upload functionality using HTML, Tailwind CSS v3, and JavaScript involves integrating several components. Below is a comprehensive guide along with the complete code to help you build this application.
Overview
- HTML Structure: Define the layout of the video player, playlist management, and the upload button.
- Tailwind CSS: Utilize Tailwind classes for styling to ensure a modern and responsive design.
- JavaScript Functionality:
- Video Playback: Handle video selection and playback.
- Playlist Management: Allow users to create, delete, and manage playlists.
- Video Upload: Enable users to upload video files to the site for playback.
Step-by-Step Implementation
1. Setting Up the HTML Structure
Create an index.html file and include the Tailwind CSS CDN for quick setup. If you're working on a production project, consider installing Tailwind via npm for better performance and customization.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Stylish Video Player with Playlists</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<!-- Container -->
<div class="w-full max-w-4xl bg-white shadow-lg rounded-lg overflow-hidden">
<div class="flex flex-col md:flex-row">
<!-- Video Player Section -->
<div class="w-full md:w-2/3 p-4">
<video id="videoPlayer" class="w-full h-64 bg-black" controls>
<source id="videoSource" src="" type="video/mp4">
Your browser does not support the video tag.
</video>
<!-- Upload Button -->
<div class="mt-4">
<label class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 cursor-pointer">
Add Video
<input type="file" id="videoUpload" accept="video/*" class="hidden">
</label>
</div>
</div>
<!-- Playlist Section -->
<div class="w-full md:w-1/3 bg-gray-50 p-4">
<h2 class="text-xl font-semibold mb-4">Playlists</h2>
<div id="playlistContainer" class="space-y-2">
<!-- Playlists will be dynamically added here -->
</div>
<!-- Add Playlist Button -->
<button id="addPlaylistBtn" class="mt-4 w-full px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700">
Add Playlist
</button>
</div>
</div>
</div>
<!-- Playlist Modal -->
<div id="playlistModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
<div class="bg-white rounded-lg p-6 w-80">
<h3 class="text-lg font-semibold mb-4">New Playlist</h3>
<input type="text" id="playlistName" class="w-full border border-gray-300 rounded px-3 py-2 mb-4" placeholder="Playlist Name">
<div class="flex justify-end">
<button id="savePlaylistBtn" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Save</button>
<button id="closeModalBtn" class="ml-2 px-4 py-2 bg-gray-300 text-gray-700 rounded hover:bg-gray-400">Cancel</button>
</div>
</div>
</div>
<!-- Tailwind CSS and JavaScript -->
<script>
// JavaScript Code Goes Here
</script>
</body>
</html>
2. Adding JavaScript Functionality
Place the following JavaScript code inside the <script> tag in your HTML. This script handles video uploads, playlist creation/deletion, and video playback.
// Initialize variables
const videoPlayer = document.getElementById('videoPlayer');
const videoSource = document.getElementById('videoSource');
const videoUpload = document.getElementById('videoUpload');
const addPlaylistBtn = document.getElementById('addPlaylistBtn');
const playlistModal = document.getElementById('playlistModal');
const closeModalBtn = document.getElementById('closeModalBtn');
const savePlaylistBtn = document.getElementById('savePlaylistBtn');
const playlistNameInput = document.getElementById('playlistName');
const playlistContainer = document.getElementById('playlistContainer');
let playlists = {}; // Object to hold playlists and their videos
// Function to open the playlist creation modal
addPlaylistBtn.addEventListener('click', () => {
playlistNameInput.value = '';
playlistModal.classList.remove('hidden');
});
// Function to close the playlist modal
closeModalBtn.addEventListener('click', () => {
playlistModal.classList.add('hidden');
});
// Function to save a new playlist
savePlaylistBtn.addEventListener('click', () => {
const name = playlistNameInput.value.trim();
if (name && !playlists[name]) {
playlists[name] = [];
renderPlaylists();
playlistModal.classList.add('hidden');
} else {
alert('Please enter a unique playlist name.');
}
});
// Function to render the playlists
function renderPlaylists() {
playlistContainer.innerHTML = '';
for (const [playlistName, videos] of Object.entries(playlists)) {
const playlistDiv = document.createElement('div');
playlistDiv.classList.add('bg-white', 'shadow', 'rounded', 'p-2', 'flex', 'justify-between', 'items-center');
const title = document.createElement('span');
title.textContent = playlistName;
title.classList.add('font-medium');
const deleteBtn = document.createElement('button');
deleteBtn.innerHTML = '✕'; // Cross mark
deleteBtn.classList.add('text-red-500', 'font-bold', 'hover:text-red-700');
deleteBtn.addEventListener('click', () => {
if (confirm(`Delete playlist "${playlistName}"?`)) {
delete playlists[playlistName];
renderPlaylists();
}
});
playlistDiv.appendChild(title);
playlistDiv.appendChild(deleteBtn);
// Add Videos within the Playlist
const videoList = document.createElement('ul');
videoList.classList.add('mt-2', 'pl-4', 'list-disc');
videos.forEach((video, index) => {
const videoItem = document.createElement('li');
videoItem.classList.add('flex', 'justify-between', 'items-center', 'mt-1');
const videoTitle = document.createElement('span');
videoTitle.textContent = video.name;
const playBtn = document.createElement('button');
playBtn.textContent = 'Play';
playBtn.classList.add('text-blue-500', 'hover:underline', 'mr-2');
playBtn.addEventListener('click', () => {
playVideo(video.url);
});
const removeVideoBtn = document.createElement('button');
removeVideoBtn.textContent = 'Remove';
removeVideoBtn.classList.add('text-red-500', 'hover:underline');
removeVideoBtn.addEventListener('click', () => {
if (confirm(`Remove "${video.name}" from "${playlistName}"?`)) {
playlists[playlistName].splice(index, 1);
renderPlaylists();
}
});
videoItem.appendChild(videoTitle);
videoItem.appendChild(playBtn);
videoItem.appendChild(removeVideoBtn);
videoList.appendChild(videoItem);
});
// Button to add a video to the playlist
const addVideoBtn = document.createElement('button');
addVideoBtn.textContent = 'Add Video';
addVideoBtn.classList.add('mt-2', 'px-2', 'py-1', 'bg-green-500', 'text-white', 'rounded', 'hover:bg-green-600');
addVideoBtn.addEventListener('click', () => {
videoUpload.click();
// After selecting a file, add it to the playlist
videoUpload.onchange = (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
playlists[playlistName].push({ name: file.name, url });
renderPlaylists();
e.target.value = ''; // Reset the input
}
};
});
playlistDiv.appendChild(videoList);
playlistDiv.appendChild(addVideoBtn);
playlistContainer.appendChild(playlistDiv);
}
}
// Function to handle video upload
videoUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
playVideo(url);
e.target.value = ''; // Reset the input
}
});
// Function to play a video
function playVideo(url) {
videoSource.src = url;
videoPlayer.load();
videoPlayer.play();
}
// Initial render
renderPlaylists();
3. Explanation of the JavaScript Code
-
Variables Initialization:
- Elements: Grabs references to essential DOM elements such as the video player, upload button, playlist container, etc.
- Playlists Object: Maintains playlists as keys with arrays of video objects
{ name, url } as their values.
-
Playlist Modal Handling:
- Open Modal: Clicking the "Add Playlist" button displays the modal for creating a new playlist.
- Close Modal: Cancelling or closing the modal hides it.
- Save Playlist: Validates and saves a new playlist, then re-renders the playlist section.
-
Rendering Playlists:
- Iterates over the
playlists object to display each playlist.
- For each playlist:
- Title and Delete Button: Shows the playlist name and a button to delete the playlist.
- Video List: Lists all videos in the playlist with options to play or remove each video.
- Add Video Button: Allows users to add videos to the playlist by uploading files.
-
Video Upload Handling:
- Global Upload: Uploading via the main "Add Video" button plays the video directly.
- Playlist-specific Upload: Uploading via a playlist's "Add Video" button adds the video to that specific playlist.
-
Playing Videos:
- Updates the video player's source to the selected video's URL and plays it.
-
Utility Function:
URL.createObjectURL(file): Creates a temporary URL for the uploaded video file to be played directly in the browser.
4. Enhancements and Considerations
-
Data Persistence: Currently, playlists and uploaded videos are stored in-memory. To persist data across sessions, consider using localStorage or integrating a backend with a database.
Example using localStorage:
// Save playlists to localStorage
function saveToLocal() {
localStorage.setItem('playlists', JSON.stringify(playlists));
}
// Load playlists from localStorage
function loadFromLocal() {
const stored = localStorage.getItem('playlists');
if (stored) {
playlists = JSON.parse(stored);
renderPlaylists();
}
}
// Call loadFromLocal on page load
window.onload = loadFromLocal;
// Modify renderPlaylists to call saveToLocal after rendering
function renderPlaylists() {
// ... existing code ...
saveToLocal();
}
// Similarly, call saveToLocal after adding/removing videos
-
Responsive Design: Tailwind's utility classes ensure responsiveness, but you can further customize breakpoints and styles as needed.
-
Security Considerations: When allowing file uploads, always be cautious about the types and sizes of files accepted, especially if integrating with a backend.
-
Improved UI/UX: You can enhance the user interface by adding animations, better modal designs, and more interactive elements using Tailwind's extensive class options.
5. Complete HTML with Embedded JavaScript
For convenience, here's the complete HTML file with embedded JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Stylish Video Player with Playlists</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<!-- Container -->
<div class="w-full max-w-4xl bg-white shadow-lg rounded-lg overflow-hidden">
<div class="flex flex-col md:flex-row">
<!-- Video Player Section -->
<div class="w-full md:w-2/3 p-4">
<video id="videoPlayer" class="w-full h-64 bg-black" controls>
<source id="videoSource" src="" type="video/mp4">
Your browser does not support the video tag.
</video>
<!-- Upload Button -->
<div class="mt-4">
<label class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 cursor-pointer">
Add Video
<input type="file" id="videoUpload" accept="video/*" class="hidden">
</label>
</div>
</div>
<!-- Playlist Section -->
<div class="w-full md:w-1/3 bg-gray-50 p-4">
<h2 class="text-xl font-semibold mb-4">Playlists</h2>
<div id="playlistContainer" class="space-y-2">
<!-- Playlists will be dynamically added here -->
</div>
<!-- Add Playlist Button -->
<button id="addPlaylistBtn" class="mt-4 w-full px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700">
Add Playlist
</button>
</div>
</div>
</div>
<!-- Playlist Modal -->
<div id="playlistModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
<div class="bg-white rounded-lg p-6 w-80">
<h3 class="text-lg font-semibold mb-4">New Playlist</h3>
<input type="text" id="playlistName" class="w-full border border-gray-300 rounded px-3 py-2 mb-4" placeholder="Playlist Name">
<div class="flex justify-end">
<button id="savePlaylistBtn" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Save</button>
<button id="closeModalBtn" class="ml-2 px-4 py-2 bg-gray-300 text-gray-700 rounded hover:bg-gray-400">Cancel</button>
</div>
</div>
</div>
<!-- Tailwind CSS and JavaScript -->
<script>
// Initialize variables
const videoPlayer = document.getElementById('videoPlayer');
const videoSource = document.getElementById('videoSource');
const videoUpload = document.getElementById('videoUpload');
const addPlaylistBtn = document.getElementById('addPlaylistBtn');
const playlistModal = document.getElementById('playlistModal');
const closeModalBtn = document.getElementById('closeModalBtn');
const savePlaylistBtn = document.getElementById('savePlaylistBtn');
const playlistNameInput = document.getElementById('playlistName');
const playlistContainer = document.getElementById('playlistContainer');
let playlists = {}; // Object to hold playlists and their videos
// Function to open the playlist creation modal
addPlaylistBtn.addEventListener('click', () => {
playlistNameInput.value = '';
playlistModal.classList.remove('hidden');
});
// Function to close the playlist modal
closeModalBtn.addEventListener('click', () => {
playlistModal.classList.add('hidden');
});
// Function to save a new playlist
savePlaylistBtn.addEventListener('click', () => {
const name = playlistNameInput.value.trim();
if (name && !playlists[name]) {
playlists[name] = [];
renderPlaylists();
playlistModal.classList.add('hidden');
} else {
alert('Please enter a unique playlist name.');
}
});
// Function to render the playlists
function renderPlaylists() {
playlistContainer.innerHTML = '';
for (const [playlistName, videos] of Object.entries(playlists)) {
const playlistDiv = document.createElement('div');
playlistDiv.classList.add('bg-white', 'shadow', 'rounded', 'p-2', 'flex', 'flex-col');
const header = document.createElement('div');
header.classList.add('flex', 'justify-between', 'items-center');
const title = document.createElement('span');
title.textContent = playlistName;
title.classList.add('font-medium', 'text-lg');
const deleteBtn = document.createElement('button');
deleteBtn.innerHTML = '✕'; // Cross mark
deleteBtn.classList.add('text-red-500', 'font-bold', 'hover:text-red-700');
deleteBtn.addEventListener('click', () => {
if (confirm(`Delete playlist "${playlistName}"?`)) {
delete playlists[playlistName];
renderPlaylists();
}
});
header.appendChild(title);
header.appendChild(deleteBtn);
playlistDiv.appendChild(header);
// Add Videos within the Playlist
const videoList = document.createElement('ul');
videoList.classList.add('mt-2', 'pl-4', 'list-disc');
videos.forEach((video, index) => {
const videoItem = document.createElement('li');
videoItem.classList.add('flex', 'justify-between', 'items-center', 'mt-1');
const videoTitle = document.createElement('span');
videoTitle.textContent = video.name;
const buttonsDiv = document.createElement('div');
const playBtn = document.createElement('button');
playBtn.textContent = 'Play';
playBtn.classList.add('text-blue-500', 'hover:underline', 'mr-2');
playBtn.addEventListener('click', () => {
playVideo(video.url);
});
const removeVideoBtn = document.createElement('button');
removeVideoBtn.textContent = 'Remove';
removeVideoBtn.classList.add('text-red-500', 'hover:underline');
removeVideoBtn.addEventListener('click', () => {
if (confirm(`Remove "${video.name}" from "${playlistName}"?`)) {
playlists[playlistName].splice(index, 1);
renderPlaylists();
}
});
buttonsDiv.appendChild(playBtn);
buttonsDiv.appendChild(removeVideoBtn);
videoItem.appendChild(videoTitle);
videoItem.appendChild(buttonsDiv);
videoList.appendChild(videoItem);
});
playlistDiv.appendChild(videoList);
// Button to add a video to the playlist
const addVideoBtn = document.createElement('button');
addVideoBtn.textContent = 'Add Video';
addVideoBtn.classList.add('mt-2', 'px-2', 'py-1', 'bg-green-500', 'text-white', 'rounded', 'hover:bg-green-600');
addVideoBtn.addEventListener('click', () => {
videoUpload.click();
// After selecting a file, add it to the playlist
videoUpload.onchange = (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
playlists[playlistName].push({ name: file.name, url });
renderPlaylists();
e.target.value = ''; // Reset the input
}
};
});
playlistDiv.appendChild(addVideoBtn);
playlistContainer.appendChild(playlistDiv);
}
}
// Function to handle video upload
videoUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
playVideo(url);
e.target.value = ''; // Reset the input
}
});
// Function to play a video
function playVideo(url) {
videoSource.src = url;
videoPlayer.load();
videoPlayer.play();
}
// Initial render
renderPlaylists();
</script>
</body>
</html>
Final Thoughts
This implementation provides a foundational structure for a video player with playlist management and upload capabilities. Depending on your requirements, you can extend this further by adding features such as:
- Drag and Drop: Allow users to reorder videos within playlists via drag-and-drop interfaces.
- Search Functionality: Implement search within playlists to quickly find videos.
- User Authentication: Secure playlists per user if integrating with a backend.
- Enhanced Styling: Customize Tailwind themes for a unique look and feel.
Tailwind CSS offers immense flexibility, making it straightforward to adapt the design to your specific needs. Additionally, JavaScript can be expanded or optimized based on the complexity and scalability you require.