html实现视频会议 (web端+手机端),支持多人在线,窗口切换,分享 - Cover 七牛

18 篇文章 0 订阅
8 篇文章 0 订阅

站在巨人的肩膀上,总是简单一些,但是看文档还有写这些demo还是用了1天的时间

电脑端:

本地窗口

 

远程窗口切换

 

手机端:

       

源码:server部分用了php,换成 java 或  python 都是可以的

<?php
	header("Content-type:text/html;charset=utf8");
	//获取room 和 title
	$room = @$_GET["room"];
	$title = @$_GET["title"];
	$uid = time(); //访问时间设置为uid
	//获取roomtoken
	if(empty($room)||empty($title)){
		echo "failure";
		exit;
	}
	$room = "zt".$room;
	$uid = "ztu".$uid;
	$expireTime = time()+60*60*24; //1天后过期
	//test
	$appid = "七牛appid"; //申请地址:https://doc.qnsdk.com/rtn
	$secretKey = "七牛sk";
	$accessKey = "七牛ak";

	//base64  以下为php版校验key
	function base64_urlSafeEncode($data) {
		$find = array('+', '/');
		$replace = array('-', '_');
		return str_replace($find, $replace, base64_encode($data));
	}	
	
	//data
	$tokenDesc = array(
		'appId' => $appid,
		'roomName' => $room,
		'userId' => $uid,
		'expireAt' => $expireTime,
		'permission' => "user"
	);
	$tokenDescJson = json_encode($tokenDesc);
	$encodedTokenDesc = base64_urlSafeEncode($tokenDescJson);
	$sign = hash_hmac('sha1', $encodedTokenDesc, $secretKey, TRUE);
	$encodedSign = base64_urlSafeEncode($sign);
	$roomToken = $accessKey.":".$encodedSign.":".$encodedTokenDesc;
?>
<html>
<head>
    <title><?php echo $title; ?>-视频会议</title>
    <meta name="decorator" content="default"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
    <style type="text/css">
        body{position: relative;margin:0;padding:0;overflow:hidden;background-color:#212121;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}*{box-sizing:border-box;margin: 0px;padding: 0px;}
        html,body{width: 100%;height: 100%;}
        .bigd{width: 100%;height: 100%}
        .title{position: absolute;left: 0px; top: 10px; color: #fff; font-size: 14px; width:100%; text-align:center; }
        .fqr{position: absolute;right: 10px; top: 10px; color: #fff; font-size: 14px;}
		.wjt{outline: none;width: 50px; height:50px; border-radius: 50px; border: none; background: url("./wjt.png")  no-repeat; background-size: 50px 50px;position: absolute;bottom: 10px; left: 48%;}
        .gd{outline: none;width: 50px; height:50px; border-radius: 50px; border: none; background: url("./gd.png")  no-repeat; background-size: 50px 50px;position: absolute;bottom: 10px; left: 48%;}
        .mylist{width: 100%;height: 5rem;position: absolute;left: 0px;bottom: 70px;}
        .list{display: block; height: 5rem;}
        .list .one{width: 100px;height: 100%; margin-left: 10px; float: left; position: relative;background-color: #4a4a4a; cursor: pointer}
        .one_video{width: 100%; height: 100%;}
        .one_text{position: absolute; left: 0px; width: 100%;bottom: 5px;width: 100%; text-align: center; color: #fff;font-size: 12px;}
        #share-2{position: absolute;left: 10px; bottom: 10px;}
		video{width:100%; height:100%};
		audio{visibility:hidden}
		.max{position: fixed!important; z-index: -1!important;left: 0px!important; top: 0px!important; width: 100%!important; height: 100%!important;bottom: auto!important;background-color:#212121!important;}
		.max .one_text{display:none;}
	</style>
</head>
<body onbeforeunload="checkLeave()">

<!--房间标题-->
<div class="title"><?php echo $title; ?></div>
<!--挂断按钮-->
<button class="wjt" onclick="myleave(this)"></button>
<!--已在线人-->
<div class="mylist">
    <div class="list">
		<div class="one max" onclick="swithDialog('local')">
		    <div class="one_video" id="videolocal" ></div>
		    <div class="one_text">本机</div>
		</div>
    </div>
</div>
<!--分享-->
<script type="text/javascript" src="./jquery-1.10.2.js"></script>
<link rel="stylesheet" href="./share.min.css" />
<script type="text/javascript" src="./jquery.share.min.js?V4"></script>
<div id="share-2"></div>
<script type="text/javascript">
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { } else {
   $('#share-2').share({sites: ['qq', 'weibo','wechat']});
}   
</script>
<script type="text/javascript" src="./scrollBar.js"></script>
<script type="text/javascript" src="./pili-rtc-web.js"></script>
<script>
	var RoomToken = '<?php echo $roomToken; ?>';
    // 确认引入成功
    console.log("current version", QNRTC.version);
	var myRoom_this = null;
    // 这里采用的是 async/await 的异步方案,您也可以根据需要或者习惯替换成 Promise 的写法
    async function joinRoom() {
		$(".wjt").attr("class","gd");
	
        // 初始化一个房间 Session 对象, 这里使用 Stream 模式
        const myRoom = new QNRTC.StreamModeSession();
		myRoom_this = myRoom;
        // 这里替换成刚刚生成的 RoomToken
        await myRoom.joinRoomWithToken(RoomToken);
        console.log("joinRoom success!");
        //显示已连接的流视频
        autoSubscribe(myRoom);
		//播放本地视频摄像头
		try{
			await publish(myRoom);
		}catch(e){
			alert("权限获取失败,请允许摄像头访问");
		}
    }
    // 增加一个函数 publish,用于采集并发布自己的媒体流
    // 这里的参数 myRoom 是指刚刚加入房间时初始化的 Session 对象
    async function publish(myRoom) {
        // 调用采集模块采集本地的音频和视频数据,返回一个包含这些数据的 Stream 对象
        const localStream = await QNRTC.deviceManager.getLocalStream({
            audio: { enabled: true },
            video: { enabled: true },
        });
        // 将刚刚的 Stream 对象发布到房间中
        await myRoom.publish(localStream);
        console.log("publish success!");
        // 在这里添加
        // 获取页面上的一个元素作为播放画面的父元素
        const localElement = document.getElementById("videolocal");
        // 调用 Stream 对象的 play 方法在这个元素下播放媒体流,其中第二个参数代表 静音播放
        localStream.play(localElement, true);
    }
	
	if(confirm("我们将采集您的摄像头/麦克风数据并与房间其他用户进行音视频通话")){
		 joinRoom();//启动
	}	
	// 这里的参数 myRoom 是指刚刚加入房间时初始化的 Session 对象, 同上
    // userId 是指订阅对象的用户名
    async function subscribe(myRoom, userId) {
		//判断是否存在该视频
		if($("#video"+userId).length==0){
			 var html = '<div class="one" onclick="swithDialog(\''+userId+'\')">\n' +
			'            <div class="one_video"  id="video'+userId+'" ></div>\n' +
			'            <div class="one_text">'+userId+'</div>\n' +
			'        </div>';
			$(".list").append(html);
		}
		// 调用订阅方法发起订阅,成功会返回一个 Stream 对象,这就是远端的流了
		const remoteStream = await myRoom.subscribe(userId);
		// 选择页面上的一个元素作为父元素,播放远端的流
		const remoteElement = document.getElementById("video"+userId);
		remoteStream.play(remoteElement);
    }

    // 这里的参数 myRoom 是指刚刚加入房间时初始化的 Session 对象, 同上
    function autoSubscribe(myRoom) {
        const users = myRoom.users;
        console.log("room current users", users)
        // 遍历房间当前所有用户
        
        for (const user of users) {
            // 如果存在一个用户,用户名不是自己并且已经发布
            // 就可以发起订阅了
            if (user.userId !== myRoom.userId && user.published) {
                // 调用我们刚刚编写的 subscribe 方法
                // 注意这里我们没有使用 async/await,而是使用了 Promise,大家可以思考一下为什么
                subscribe(myRoom, user.userId)
                    .then(() => console.log("subscribe success!"))
            .catch(e => console.error("subscribe error", e));
            }
        }
		//width change
        $(".list").css("width",$(".list .one").length*110+"px");
        $(".mylist").scrollBar();

        // 接下来我们需要处理第二种情况,也就是监听事件来感知房间内有用户发布了
        myRoom.on("user-publish", (user) => {
            console.log("user", user.userId, "is published!");
			subscribe(myRoom, user.userId)
				.then(() => console.log("subscribe success!"))
				.catch(e => console.error("subscribe error", e));
        });
		
		//远程用户离开房间
		myRoom.on("user-leave",(user)=>{
			console.log("user", user.userId, "go leave!");
			if($("#video"+user.userId).length>0){//存在用户
				$("#video"+user.userId).parent(".one").remove();//去掉UI
			}
		});
    }
	
	//离开房间
	function myleave(obj){
		$(obj).attr("class","wjt");
		myRoom_this.leaveRoom();
	}
	
	//交换窗口
	function swithDialog(zid){
		$("#video"+zid).parent(".one").addClass("max");
		$("#video"+zid).parent(".one").siblings().removeClass("max");
	}
	
	//离开页面
	function checkLeave(){
		myRoom_this.leaveRoom();
	}
</script>
</body>
</html>

未做太多业务的处理,只是简单的视频,未涉及文件传递或聊天socket功能!

有想法的可以研究,研究啦~

百度网盘:https://pan.baidu.com/s/1jv8dppNcjc3gpffGQnWVGw  提取码:67jz

感谢您的支持,写的文章如对您有所帮助,开源不易,请您打赏,谢谢啦~

  • 17
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,为了实现火影忍者的背景切换登录页面,我们需要用到HTML、CSS和JS。首先,我们需要准备好背景图片,这里我选择了几张火影忍者的图片。 接下来,我们可以开始编写代码了。 HTML代码: ```html <!DOCTYPE html> <html> <head> <title>火影忍者登录页面</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="bg-img"></div> <div class="container"> <form> <h1>火影忍者登录</h1> <input type="text" placeholder="用户名"> <input type="password" placeholder="密码"> <button type="submit">登录</button> </form> </div> </body> </html> ``` 我们在页面中添加了一个id为“bg-img”的div,用来显示背景图片。我们还添加了一个class为“container”的div,用来包含登录表单。 CSS代码: ```css body { margin: 0; padding: 0; background-color: #000; } #bg-img { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; background-size: cover; background-position: center; animation: bg-slide 20s infinite; } @keyframes bg-slide { 0% { background-image: url("bg1.jpg"); } 25% { background-image: url("bg2.jpg"); } 50% { background-image: url("bg3.jpg"); } 75% { background-image: url("bg4.jpg"); } 100% { background-image: url("bg5.jpg"); } } .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(255, 255, 255, 0.8); padding: 30px; border-radius: 5px; box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); } h1 { text-align: center; font-size: 32px; margin-bottom: 20px; } input { display: block; width: 100%; padding: 10px; margin-bottom: 20px; font-size: 16px; border: none; border-radius: 5px; box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3); } button { display: block; width: 100%; padding: 10px; background-color: #c9302c; color: #fff; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; } button:hover { background-color: #a52725; } ``` 在CSS中,我们设置了背景图片的位置、大小和动画。我们使用了@keyframes来实现背景图片的切换,每25%的时间显示一张不同的图片。我们还设置了登录表单的样式,包括位置、颜色、边框和阴影等。 JS代码: ```javascript window.onload = function() { var bgImg = document.getElementById("bg-img"); var bgImgs = ["bg1.jpg", "bg2.jpg", "bg3.jpg", "bg4.jpg", "bg5.jpg"]; var index = 0; setInterval(function() { index++; if (index == bgImgs.length) { index = 0; } bgImg.style.backgroundImage = "url(" + bgImgs[index] + ")"; }, 20000); } ``` 在JS中,我们使用了一个定时器来实现背景图片的切换。我们设置了一个数组来存储背景图片的路径,然后每隔20秒就切换一次背景图片。 最后,我们将HTML、CSS和JS保存到同一个文件夹中,然后我们就可以在浏览器中打开这个文件来查看火影忍者的背景切换登录页面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值