Jquery解决视频播放过程中定时弹出确认窗口


前言

本系列文章主要记录项目过程中重点的项目技术


一、目标

因为业务的需要,我们需要在视频正常播放的过程中,间隔的弹出确认窗口,从而避免用户刷视频,实现的效果如下
在这里插入图片描述

二、实现目标

1.通过video标签,播放视频

<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="content-type" content="text/html; charset=UTF-8">
  <title>Document</title>
</head>
<body>
<button class="gonext"> 播放</button>
<button class="gonext1"> 暂停</button>
<button class="getTime">获取总时长<span></span></button>
  <video webkit-playsinline="true" x-webkit-airplay="true" playsinline="true" x5-video-player-type="h5" id="video" src="ringVideo.mp4" onended="myFunction()"></video>
  <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>

2.编写播放、暂停等函数

调用getConfig()方法通过属性名获得属性值

  var lable = false;
  //当前时间
  var currentTime = 0;
  //总时长
  var  duration = 0; 
  // 音频播放结束
    function myFunction(){  
      setTimeout(function(){
        console.log("videoTime")
      },0);
    }
    $(function(){
      // 播放
      $(".gonext").click(function(){
		lable = true;
        $("video")[0].play();
		setTimeout(videoPopupWindow,10000);//这里设置的是3秒后触发事件
      })
      // 暂停
      $(".gonext1").click(function(){
		lable = false;
        $("video")[0].pause();
      })
      // 音频播放结束事件
      /*$("video").on('ended', function() {
        setTimeout(function(){
          console.log("videoTime")
        },0);
      })*/
      //获取视频的总时长
      $(".getTime").click(function(){
        var videoTime=Math.floor($("video")[0].duration); 
        console.log(videoTime)
      })
    })

3.获得当前时间和总时长

	$(document).ready(function(){	
	  $("#video").on(
		"timeupdate", 
		function(event){
		  currentTime = this.currentTime;
		  duration = this.duration;
		});
	});

4.添加弹窗事件

//视频广告弹窗
	function videoPopupWindow(){
		//判断函数执行的时候是否被暂停了
		if(lable){
			//暂停播放
			$("video")[0].pause();
			var person=prompt("请输入字符XIAOMING","Harry Potter");
			if (person!=null && person!=""){
				if(person=="XIAOMING"){
				$("video")[0].play();
				//判断距离结束的时间是否超过定时间隔
				if(duration - currentTime >= 10){
					setTimeout(videoPopupWindow,10000);//这里设置的是3秒后触发事件
					console.log(11);
					}
					
				}else{
					alert("输入的字符不对,请重新输入");
					videoPopupWindow();
				}
			}else{
				alert("不能为空");
				videoPopupWindow();
			}
		}
		
		
	}

5.完整代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="content-type" content="text/html; charset=UTF-8">
  <title>Document</title>
</head>
<body>
<button class="gonext"> 播放</button>
<button class="gonext1"> 暂停</button>
<button class="getTime">获取总时长<span></span></button>
  <video webkit-playsinline="true" x-webkit-airplay="true" playsinline="true" x5-video-player-type="h5" id="video" src="ringVideo.mp4" onended="myFunction()"></video>
  <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
  <script type="text/javascript">
  var lable = false;
  //当前时间
  var currentTime = 0;
  //总时长
  var  duration = 0; 
  // 音频播放结束
    function myFunction(){  
      setTimeout(function(){
        console.log("videoTime")
      },0);
    }
    $(function(){
      // 播放
      $(".gonext").click(function(){
		lable = true;
        $("video")[0].play();
		setTimeout(videoPopupWindow,10000);//这里设置的是3秒后触发事件
      })
      // 暂停
      $(".gonext1").click(function(){
		lable = false;
        $("video")[0].pause();
      })
      // 音频播放结束事件
      /*$("video").on('ended', function() {
        setTimeout(function(){
          console.log("videoTime")
        },0);
      })*/
      //获取视频的总时长
      $(".getTime").click(function(){
        var videoTime=Math.floor($("video")[0].duration); 
        console.log(videoTime)
      })
    })
	
	$(document).ready(function(){
			
	  $("#video").on(
		"timeupdate", 
		function(event){
		  currentTime = this.currentTime;
		  duration = this.duration;
		});
	});
	
	//视频广告弹窗
	function videoPopupWindow(){
		//判断函数执行的时候是否被暂停了
		if(lable){
			//暂停播放
			$("video")[0].pause();
			var person=prompt("请输入字符XIAOMING","Harry Potter");
			if (person!=null && person!=""){
				if(person=="XIAOMING"){
				$("video")[0].play();
				//判断距离结束的时间是否超过定时间隔
				if(duration - currentTime >= 10){
					setTimeout(videoPopupWindow,10000);//这里设置的是3秒后触发事件
					console.log(11);
					}
					
				}else{
					alert("输入的字符不对,请重新输入");
					videoPopupWindow();
				}
			}else{
				alert("不能为空");
				videoPopupWindow();
			}
		}
		
		
	}
  </script>
</body>
</html>

总结

至此,本文章结束

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值