ブラウザで表示されているVideoの再生をjavascriptで制御します。
ファイルのビデオ再生アプリの作成
ファイルのビデオ再生アプリを次のように作成します。javascriptでの制御については、「HTMLMediaElement」を参考にして作成します。
videoplay/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>動画再生</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-6">
<video id="video01" preload="auto" controlsList="nodownload" oncontextmenu="return false;"
src="./video/-152740.mp4" type="video/mp4">
</video>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 0%" aria-valuenow="15" aria-valuemin="0"
aria-valuemax="100"></div>
</div>
<input type="range" class="form-range" id="data-slider">
<button id="play" type="button" class="btn btn-outline-primary">▷</button>
<button id="stop" type="button" class="btn btn-outline-primary">||</button>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
videoplay/js/main.js
let video;
let updateBar;
let speedvalue = 1
$(function () {
video = $('#video01').get(0);
$(".progress-bar").attr("style", "width: " + "0%");
});
$('#play').click(function () {
console.log("play");
//const newRate = (video.playbackRate + speedvalue).toFixed(1);
video.playbackRate = speedvalue;
video.play();
updateBar = setInterval(update, 30);
});
$('#stop').click(function () {
console.log("stop");
window.clearInterval(updateBar);
video.pause();
});
$('#data-slider').on('input', function () {
let val = $(this).val();
//console.log('data-slider00' + val);
let period = video.duration * val / 100
var size = parseInt(period * 100 / video.duration);
$(".progress-bar").attr("style", "width: " + size + "%");
playtime = period;
video.currentTime = period;
});
function update() {
console.log("update");
//console.log(video.currentTime);
if (!video.ended) {
playtime = video.currentTime
//console.log('duration: ' + video.duration + ' currentTime: ' + video.currentTime);
var size = parseInt(video.currentTime * 100 / video.duration);
$(".progress-bar").attr("style", "width: " + size + "%");
} else {
playtime = video.currentTime
$(".progress-bar").attr("style", "width: " + "100%");
//console.log($(".progress-bar").attr("style"));
video.currentTime = 0;
playtime = 0;
window.clearInterval(updateBar);
}
}
ファイルのビデオ再生アプリの実行
URL「http://localhost/videoplay/」をアクセスし、再生ボタンやpauseボタンを押すと、画像の再生が制御できます。また、スライダを動かすことにより、ビデオの生成が制御できます。
