插件描述:Danmmu Player是一个具备弹幕功能的Html5视频播放器。 具备弹幕视频播放,接受用户发送彩色弹幕,实时调解弹幕显示效果等功能。 Danmmu Player意在使开发者能便捷的在网站中实现弹幕视频播放。
Danmmu Player动态连接到数据库示例:
插入该插的css、js文件。
<div id="danmup"></div>
配置参数:
<script>
$("#danmup").DanmuPlayer({
islogin:<?PHP echo($islogin); ?>,//自建属性
poster:"<?PHP if($obj->ImageURL==""){?>images/noImg.jpg<?PHP }else{?>user/images/<?PHP echo($obj->ImageURL); ?><?PHP } ?>",//自建属性
src:"user/video/abc.mp4",//视频源
height: "480px", //区域的高度
width: "100%" ,//区域的宽度
urlToGetDanmu:"query.php",//从后端获取弹幕数据
urlToPostDanmu:"stone.php",//发送弹幕数据给后端保存入库
gd:<?PHP echo($gid); ?>,//自建属性
ud:"<?PHP echo($UserName); ?>",//自建属性
ndmid:ndmid,//自建属性
});
</script>
js中数据库参数处理部分:
//从后端获取弹幕
this.getDanmu = function () {
$.get(that.options.urlToGetDanmu,
{
GoodsId: that.options.gd,
ndmid:that.options.ndmid,
},
function (data, status) {
danmuFromSql = eval(data);
for (var i = 0; i < danmuFromSql.length; i++) {
try {
var danmuLs = eval('(' + danmuFromSql[i] + ')');
} catch (e) {
continue;
}
$(that.id + ' .danmu-div').danmu("addDanmu", danmuLs);
}
});
};
if (options.urlToGetDanmu)
this.getDanmu();
//发送弹幕
this.sendDanmu = function (e) {
var text = $(e.data.that.id + " .danmu-input").get(0).value;
if (text.length == 0) {
return;
}
if (text.length > 100){
alert("弹幕过长!");
return;
}
text = text.replace(/&/g, ">").replace(/</g, "<").replace(/>/g, ">").replace(/\"/g, """).replace(/\n/g, "<br>");
var color = e.data.that.danmuColor;
var position = $(e.data.that.id + " input[name=danmu_position]:checked").val();
var size = $(e.data.that.id + " input[name=danmu_size]:checked").val();
var time = $(e.data.that.id + " .danmu-div").data("nowTime") + 3;
var textObj = '{ "text":"'+e.data.that.options.ud+':' + text + '","color":"' + color + '","size":"' + size + '","position":"' + position + '","time":' + time + '}';
if(e.data.that.options.islogin==0){
$("#tishi").show();
}else{
if (e.data.that.options.urlToPostDanmu)
$.post(e.data.that.options.urlToPostDanmu,
{
danmu: textObj,
GoodsId: e.data.that.options.gd,
UserId:e.data.that.options.ud,
},
function(data){}
);
textObj = '{ "text":"'+e.data.that.options.ud+':' + text + '","color":"' + color + '","size":"' + size + '","position":"' + position + '","time":' + time + ',"isnew":""}';
var newObj = eval('(' + textObj + ')');
$(e.data.that.id + " .danmu-div").danmu("addDanmu", newObj);
$(e.data.that.id + " .danmu-input").get(0).value = '';
$(e.data.that).trigger("senddanmu");
}
};
query.php文件:
<?php
header('Content-type:text/html;charset=utf8');
$conn = @ mysql_connect("localhost", "你的mysql用户名", "你的密码") or die("datebase can't been connected");
mysql_select_db("2shou", $conn);//数据库2shou
mysql_query("set names 'utf8'"); //
$gd=$_GET['GoodsId'];//从js获取数据
$ndmid=$_GET['ndmid'];//从js获取数据
$ndmidc=explode(',',$ndmid);
$sql="SELECT danmu FROM danmu where GoodsId = '" . $gd . "' and dmid not in('".$ndmidc[0]."','".$ndmidc[1]."','".$ndmidc[2]."','".$ndmidc[3]."','".$ndmidc[4]."','".$ndmidc[5]."','".$ndmidc[6]."','".$ndmidc[7]."','".$ndmidc[8]."','".$ndmidc[9]."')";
$query=mysql_query($sql);
//echo $danmu;
echo "[";
$first=0;
while($row=mysql_fetch_array($query)){
if ($first) {
echo ",";
}
$first=1;
echo "'".$row['danmu']."'";
}
echo "]";
?>
stone.php文件:
<?php
header('Content-type:text/html;charset=utf8');
date_default_timezone_set('Asia/Chongqing'); //系统时间差8小时问题
$conn = @ mysql_connect("localhost", "。。。", "。。。") or die("datebase can't been connected");
mysql_select_db("2shou", $conn);
mysql_query("set names 'utf8'");
$danmu=$_POST['danmu'];//从js获取数据
$gd=$_POST['GoodsId'];//从js获取数据
$ud=$_POST['UserId'];//从js获取数据
$sendtime=strftime("%Y-%m-%d %H:%M:%S");
$sql="INSERT INTO Danmu (GoodsId, UserId, danmu, SendTime) VALUES ('" . $gd . "','" . $ud . "','" . $danmu . "','" . $sendtime ."')";
$query=mysql_query($sql);
?>
数据库2shou结构:
完整代码见https://download.youkuaiyun.com/download/cc1314_/10209859