映像の長さ(尺)を取得する

説明

videoタグで指定された映像の長さ(尺)を取得するにはdurationプロパティを参照します。durationプロパティは読み出し専用で設定することはできません。

サンプルプログラム

【HTML】
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Sample</title>
<link rel="stylesheet" href="css/main.css" type="text/css" media="all">
<script type="text/javascript" src="js/sample.js"></script>
</head>
<body>
<h1>映像の長さ(尺)を取得する</h1>
<video id="myVideo" controls>
<source src="movie/sunflower.mov">
<source src="movie/sunflower.ogg">
</video>
<div id="result"></div>
</body>
</html>


【sample.js】
window.onload = function(){
var vObj = document.getElementById("myVideo");
document.getElementById("result").innerHTML = "映像の長さは"+vObj.duration+"秒です";
}
サンプルを実行
[戻る]