비디오 태그를 사용해서 주어진 주소 중 랜덤하게 선택해서 영상을 재생해주는 소스이다.
동영상의 HLS 방식을 사용했다.
<video id="videoPlayer" width="700" autoplay muted playsinline ></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
const videoFiles = [
"영상1.mp4/playlist.m3u8","영상2.mp4/playlist.m3u8",
];
const video = document.getElementById("videoPlayer");
// 랜덤 인덱스 선택
const randomIndex = Math.floor(Math.random() * videoFiles.length);
const selectedSource = videoFiles[randomIndex];
let hls;
function playVideo(source) {
if (hls) hls.destroy();
if (Hls.isSupported()) {
hls = new Hls();
hls.loadSource(source);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, () => {
video.muted = true;
video.play();
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = source;
video.muted = true;
video.addEventListener('loadedmetadata', () => video.play());
} else {
console.error("HLS not supported in this browser.");
}
}
// 한 번만 재생, ended 이벤트는 생략하거나 UI 제어용으로 사용
playVideo(selectedSource);
</script>
'기타' 카테고리의 다른 글
비디오 태그 영상재생 완료 후 컨트롤 노출막기 (0) | 2025.05.14 |
---|---|
플스플러스 5월 월간게임 - PS PLUS (0) | 2025.05.14 |
비디오 태그로 HLS 영상 여러개 재생하기 (0) | 2025.05.12 |
두개의 조건을 한번에 쿼리하기 - case when (0) | 2025.05.12 |
디데이 계산하기 (0) | 2025.04.23 |