html5開發 視頻字幕的程序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>使用video實現實時字幕的效果</title>
    <style>
        #box{
            border: 1px solid red;
            width: 400px;
            height: 400px;
            position: absolute;
        }
        video{
            position: relative;
        }
        #caption{
            position: relative;
            left: 180px;
            top: -60px;
            font-size: 18px;
            font-weight: bold;
        }
    </style>
    <script src="js/jquery.js"></script>
    <script>
        window.addEventListener("load",function(){
            //咱們的數據數組
            var captionData=[];
            //首先得到咱們的視頻對象
            var myvideo=document.getElementByIdx_x("myvideo");
            //此項表示加載以後執行如下的操做
            //得到咱們存放字幕的地方
            var mycaption=document.getElementByIdx_x("caption");
            //讀入咱們的字幕數據
            $.get("js/captions.js",function(text,status){
                captionData=eval_r(text);
            });
 
           
            window.addEventListener("timeupdate",function(){
                var csec=myvideo.currentTime;
                //開始進行判斷
                for(var i=0;i<captionData.length;i++){
                    //根據當前播放的時間跟咱們json的時間進行比較
                    if(csec>captionData[i].start&&csec<captionData[i].end){
                         //此時將字幕設置
                        mycaption.innerHTML=captionData[i].text;
                        mycaption.style.color=captionData[i].color||"#ff0";
                    }
                }
 
            },true);
 
        },true);
 
 
 
 
 
    </script>
</head>
<body>
    <div id="box">
        <video controls width="400" id="myvideo">
            <source src="video/first.mp4">
        </video>
        <div id="caption">播放開始</div>
    </div>
</body>
</html>
相關文章
相關標籤/搜索