It looks like you're trying to implement a custom video player where subtitles are only shown when the "subtitle" button is clicked, and they should not appear initially when the video loads.
The issue you're encountering is likely due to the default attribute in your <track> element, which causes the subtitles to be displayed by default. Also, you need to manage the subtitle track's visibility using JavaScript when the "subtitle" button is clicked.
Here's how you can fix this:
- Remove the
default attribute from the <track> element to prevent subtitles from showing initially.
- Use JavaScript to toggle the subtitles when the "subtitle" button is clicked.
Let's go through these steps in detail.
1. Modify Your HTML to Remove the default Attribute
In your HTML code, the <track> element currently looks like this:
<track id="track" label="English" kind="subtitles" src="assets/subtitles/english_subtitles.vtt" default>
The default attribute causes the subtitles to display automatically when the video loads. To prevent this, simply remove the default attribute:
<track id="track" label="English" kind="subtitles" src="assets/subtitles/english_subtitles.vtt">
2. Add JavaScript to Toggle Subtitles
You'll need to add some JavaScript code to manage the subtitle track's visibility. Here's how you can do it:
a. Get References to the Video and Subtitle Button
First, in your script.js, get references to your video element, the subtitle button, and the text track:
const video = document.getElementById('videoPlayer');
const subtitleBtn = document.getElementById('subtitleBtn');
// Get the first text track (assuming there's only one)
const track = video.textTracks[0];
b. Hide Subtitles Initially
Set the text track's mode to 'hidden' to ensure subtitles are not visible when the video starts:
// Hide subtitles initially
track.mode = 'hidden';
c. Add Event Listener to the Subtitle Button
Add a click event listener to the subtitle button that toggles the subtitles on and off:
subtitleBtn.addEventListener('click', () => {
if (track.mode === 'hidden') {
// Show subtitles
track.mode = 'showing';
subtitleBtn.style.opacity = 1; // Optionally, change button appearance
} else {
// Hide subtitles
track.mode = 'hidden';
subtitleBtn.style.opacity = 0.5; // Optionally, change button appearance
}
});
d. Optional: Update Button Appearance
You might want to provide visual feedback when subtitles are on or off. In the code above, I adjusted the button's opacity, but you can change the icon or style as needed.
Complete script.js Example
Here's how your script.js file might look:
// Get elements
const video = document.getElementById('videoPlayer');
const subtitleBtn = document.getElementById('subtitleBtn');
const track = video.textTracks[0]; // Assuming only one track
// Hide subtitles initially
track.mode = 'hidden';
// Toggle subtitles on button click
subtitleBtn.addEventListener('click', () => {
if (track.mode === 'hidden') {
// Show subtitles
track.mode = 'showing';
subtitleBtn.style.opacity = 1;
} else {
// Hide subtitles
track.mode = 'hidden';
subtitleBtn.style.opacity = 0.5;
}
});
// Optionally, you can handle other controls (play/pause, mute/unmute, etc.) here as well
Explanation
-
track.mode: Controls the visibility of the text track. It can be set to:
'disabled': The track is disabled.
'hidden': The track is active but not displayed.
'showing': The track is active and displayed.
-
Event Listener: When the subtitle button is clicked, the script checks the current mode of the text track and toggles it.
-
Initial State: By setting track.mode = 'hidden' when the script runs, we ensure subtitles are not displayed until the user clicks the subtitle button.
Updated HTML and JavaScript Integration
Make sure your HTML and JavaScript files are correctly linked, and your script runs after the DOM has loaded.
In your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Climate Change</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Video Container -->
<div id="videoContainer">
<!-- Video Player -->
<video id="videoPlayer" loop playsinline webkit-playsinline>
<source src="assets/videos/Daikins Commitment English.mp4" type="video/mp4">
<!-- Subtitle track without the default attribute -->
<track id="track" label="English" kind="subtitles" src="assets/subtitles/english_subtitles.vtt">
Your browser does not support the video tag.
</video>
<div id="controls">
<!-- Control Buttons -->
<img src="assets/icons/pause.png" id="playPauseBtn" alt="playpause">
<img src="assets/icons/mute.png" id="muteUnmuteBtn" alt="muteunmute">
<img src="assets/icons/restart.png" id="restartBtn" alt="restart">
<img src="assets/icons/subtitle.png" id="subtitleBtn" alt="subtitle">
<select id="languageSelect">
<option value="default"> 🇬🇧 English</option>
<option value="japanese"> 🇯🇵 Japanese</option>
<option value="spanish"> 🇪🇸 Spanish</option>
<option value="french"> 🇫🇷 French</option>
</select>
<button id="fullscreenBtn">Fullscreen</button>
</div>
</div>
<!-- Include your script at the end of the body -->
<script src="script.js"></script>
</body>
</html>
In your script.js:
Make sure the script runs after the DOM is fully loaded. If you're placing your script at the end of the body (like in the example), you don't need to wait for the DOM to load. If your script is in the head, wrap your code in a DOMContentLoaded event listener.
Additional Tips
-
Multiple Subtitle Tracks: If you plan to have subtitles in multiple languages, you'll need to manage multiple tracks. You can adapt the script to handle switching between them based on the selected language in the <select> element.
-
Browser Support: Ensure that the browsers you are targeting support the HTML5 <video> element and text tracks.
-
Error Handling: Include error handling in your script to check if video.textTracks[0] exists before trying to access it.
By following these steps, you should be able to control the display of subtitles with the subtitle button and ensure they do not show up until the button is clicked.
Let me know if you need further assistance or have any questions!