contoh cara gunakan plyr.io
- User
contoh cara gunakan plyr.io
- Assistant
contoh cara gunakan plyr.io
Plyr is a lightweight and flexible JavaScript library used for creating customized media players, such as YouTube, Vimeo, or local HTML media players, with consistent and beautiful designs.
Below is an example of how to use Plyr.io to set up a video player:
You need to include the Plyr library in your project. You can do this by using a CDN or downloading the library files.
Add the Plyr CSS and JavaScript to your HTML file from the official CDN:
<!-- Plyr CSS -->
<link rel="stylesheet" href="https://cdn.plyr.io/3.7.8/plyr.css" />
<!-- Plyr JavaScript -->
<script src="https://cdn.plyr.io/3.7.8/plyr.js"></script>
Here’s an example of how to structure your HTML file to include a video element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plyr Example</title>
<!-- Plyr CSS -->
<link rel="stylesheet" href="https://cdn.plyr.io/3.7.8/plyr.css" />
</head>
<body>
<!-- Video Player -->
<video id="player" playsinline controls>
<source src="https://cdn.plyr.io/static/blank.mp4" type="video/mp4" />
<source src="example.webm" type="video/webm" />
<!-- Fallback content for browsers that don’t support the <video> element -->
Your browser does not support the video tag.
</video>
<!-- Plyr JS -->
<script src="https://cdn.plyr.io/3.7.8/plyr.js"></script>
<script>
// JavaScript to initialize the Plyr player
const player = new Plyr('#player');
</script>
</body>
</html>
You can customize the player using the Plyr options. For example, you can define custom controls, autoplay, or start playing a video at a certain time.
Modify the JavaScript part like this:
const player = new Plyr('#player', {
controls: ['play', 'progress', 'volume', 'fullscreen'],
autoplay: true,
muted: true
});
You can create a player for YouTube or Vimeo videos by embedding them within the same Plyr setup. For example:
<iframe
id="player"
src="https://www.youtube.com/embed/bTqVqk7FSmY?origin=www.example.com&iv_load_policy=3"
allowfullscreen
allow="autoplay"
></iframe>
<!-- Initialize Plyr -->
<script>
const player = new Plyr('#player');
</script>
The Plyr player comes with a default style, but you can add custom CSS to tweak its appearance further. Use the .plyr class to style the player.
This is how you can quickly set up and customize a media player using Plyr.io! For more advanced features, check their official documentation.