视频标签及其使用

4/25/2024 html

# 1.音视频标签

标签有哪些 属性及 属性的作用

通过js可以操作哪些属性 并 知道有什么效果

# 1.video 视频标签

controls 控制台:快进 快退 暂停播放 继续播放 下载视频按钮 播放的时间进度

autoplay: 是否进入页面的瞬间 自动播放 【如果在浏览器中使用,必须先静音】

loop:是否循环播放,如果视频播放结束,是否自动重新开发播放

muted: 是否静音

poster:视频默认的首图

<video src="视频路径(相对路径、绝对路径、互联网url地址)"  width="300"  height="500" > </video>
1

# 通过js 操作video

  • 暂停播放 pause
  • 继续播放 play
  • 重载 load
//获取video dom
let  videoDom = document.querselector('video')
videoDom.pause()
videoDom.play ()
videoDom.load()

1
2
3
4
5
6

# 通过js获取的属性

  • 视频的总时长
  • 视频当前播放的时间
//获取video dom
let  videoDom = document.querselector('video')
videoDom.duration   //总时间
videoDom.currentTime   //当前时间

1
2
3
4
5
  • 手写播放器
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            position: relative;
            width: 600px;
            height: 400px;
        }
        .crton{
            position: absolute;
            bottom: 32px;
            left: 0;
            width: 600px;
            height: 50px;
            background-color:rgba(0,0,0,0.3);
            
        }
        .progress{
            position: relative;
            width: auto;
            margin: 0 10px;
            height: 5px;
            border-radius: 2px;
    
        }
        .progress-bg{
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 5px;
            border-radius: 2px;
            background: #aaa;
        }
        .progress-active{
            position: absolute;
            left: 0;
            top: 0;
            width: 0%;
            height: 5px;
            border-radius: 2px;
            background: #00aeec;
        }
        .btns{
            width:auto;
            margin: 0 10px;
            height: 45px;
            display: flex;
            align-items: center;
        }
        img{
            width: 20px;
            height: 20px;
            margin-right: 20px;
        }
        .time{
            color: #fff;
        }
        #stop{
            display: none;
        }
    </style>
</head>
<body>
    <div class="box">
        <video src="./video/1.mp4" width="600" height="400"></video>
        <div class="crton">
            <div class="progress">
                <div class="progress-bg"></div>
                <div class="progress-active"></div>
            </div>
            <div class="btns">
                <img src="./icon/pre.png" alt="">
                <img src="./icon/stop.png" id="stop" alt="">
                <img src="./icon/play.png" id="play" alt="">
                <img src="./icon/next.png" alt="">
                <div class="time">11:00/20:00</div>
            </div>
        </div>
    </div>
    <script>
        let videoDom = document.querySelector('video');
        let timeDom = document.querySelector('.time')
        let progressActiveDom = document.querySelector('.progress-active')

        // 播放功能
        let playBtn = document.querySelector('#play')
        playBtn.addEventListener('click',function(){
            videoDom.play();
            playBtn.style.display ='none'
            stopBtn.style.display='block'
        })
        // 暂停播放
        let stopBtn = document.querySelector('#stop')
        stopBtn.addEventListener('click',function(){
            videoDom.pause();
            stopBtn.style.display ='none'
            playBtn.style.display='block'
        })

        // 时间和总时间
        setInterval(function(){
            // 当前时间 
            let currentTime =   videoDom.currentTime;
            // 总时间
            let total = videoDom.duration;

            // 处理时间
            let min =  parseInt(currentTime/60) 
            let s = parseInt(currentTime%60) 
            min= min<10?'0'+min:min
            s = s<10?'0'+s:s
            let currentTime1 = min+":"+s;

            let min1 =  parseInt(total/60) 
            let s1 = parseInt(total%60) 
            min1= min1<10?'0'+min1:min1
            s1 = s1<10?'0'+s1:s1
            let total1 = min1+":"+s1;

            
     
            timeDom.textContent = currentTime1 +'/'+total1

            // 计算进度条 的宽度  ==当前时间/总时间
            let progressNum = currentTime/total*100;
            progressActiveDom.style.width = progressNum+'%';


        },1000)





    </script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142