|
|
/* eslint-disable */
|
|
|
/**
|
|
|
* Created by qiujun on 16/6/4.
|
|
|
* 函数说明引导,部分全局参数定义在live_detail.html中
|
|
|
* init_play_button ===> 用于初始化播放按钮,直播和重播时调用init_video函数初始化video标签并进行播放,直播结束状态时显示直播结束
|
|
|
* init_video ===> 根据不同参数初始化video标签,调用了yoho_video.js的方法
|
|
|
* get_chat_info -> get_websocket_server ===> 顺序执行,用于获取websocket的ip地址
|
|
|
* link_to_websocket_server ===> 从获取到的ip地址连接websocket,并进行获取到消息的处理
|
|
|
* get_cmd ===> 根据不同的cmd(目前只用到2个发送的)获取websocket需要发送的json数据的字符串
|
|
|
* send_heart_beat ===> 每隔1分钟发送一次心跳包,以获取服务器当前是否仍然健在以及当前的观看人数
|
|
|
* insert_msg,insert_user,insert_like,insert_audience,insert_end ===>处理websocket获取到的消息,或者重播时传过来的模拟消息
|
|
|
* get_replay_chat_barrage ===> 获取重播弹幕
|
|
|
* manage_replay_chat_msg ===> 通过requestAnimationFrame每帧调用用于判断是否可以展示重播消息和点赞
|
|
|
* replay_barrage_callback_handler ===> 由video的各个事件中调取的callback方法
|
|
|
*/
|
|
|
var prefixes = 'webkit moz ms o'.split(' '); //各浏览器前缀
|
|
|
var requestAnimationFrame = window.requestAnimationFrame;
|
|
|
var cancelAnimationFrame = window.cancelAnimationFrame;
|
|
|
|
|
|
var $btn_play;
|
|
|
var video_width = 375;
|
|
|
var video_height = 667;
|
|
|
var video_id = 'yoho_live'; //视频video标签id
|
|
|
var is_live = true; //是否是直播,根据地址栏参数可以判断
|
|
|
var get_socket_server_url = '/activity/live/barrage'; //获取websocket服务器的接口
|
|
|
var get_replay_chat_url = '/activity/live/replay/barrage'; //获取重播弹幕
|
|
|
var query_arr = []; //地址栏参数数组
|
|
|
var link_type = 'webksocket'; //连接类型
|
|
|
var live_ws; //websocket
|
|
|
var heart_beat_interval; //心跳计时器
|
|
|
var live_time_interval; //播放时间计时器
|
|
|
var live_duration = 0; //播放时间计数器
|
|
|
var now_like_nums = 0; //当前点赞数
|
|
|
var $live_like_pannel; //点赞div,用于存放小脸动画
|
|
|
var is_animate = false; //用于控制动画的频率
|
|
|
|
|
|
var replay_video;
|
|
|
var replay_new_start_time = 1466591331; //根据play进度调整的当前时间,用于获取重播弹幕
|
|
|
var replay_chat_interval = 60; //重播弹幕获取的时间区间长度,接口默认是5分钟
|
|
|
var replay_chat_arr = []; //用于存储重播弹幕obj
|
|
|
var replay_chat_frame_count = 0; //用于计算执行requstAnimationFrames的次数
|
|
|
var is_replay_chat_loading = false; //是否正在调取接口
|
|
|
var can_seek = true; //
|
|
|
var replay_per_like = 0; //平均每秒增加的点赞数
|
|
|
var replay_per_request_like = 0; //平均每帧增加的点赞数(大概)
|
|
|
var replay_video_duration = 0; //重播视频
|
|
|
var replay_now_like = 0; //当前重播的点赞数
|
|
|
var is_wating = false;
|
|
|
|
|
|
var test_count = 99;
|
|
|
|
|
|
var CMD = {
|
|
|
LOGIN: 1, //客户端发送登录消息
|
|
|
SEND_MESSAGE: 2, //客户端发送消息
|
|
|
LOGOUT: 3, //客户端发送退出消息
|
|
|
NEW_USER: 4, //服务器通知新用户加入
|
|
|
RECEIVE_MESSAGE: 5, //服务器通知用户发送的消息
|
|
|
HEART_BEAT: 7, //客户端发送心跳包
|
|
|
SEND_LIKE: 8, //客户端点赞
|
|
|
RECEIVE_LIVE: 9, //服务器通知点赞
|
|
|
ONLINE_NUM: 10, //服务器通知当前在线人数
|
|
|
SEND_LIVE_END: 11, //主播app通知服务端直播结束
|
|
|
RECEIVE_LIVE_END: 12, //服务端通知直播结束
|
|
|
LOGIN_RETURN: 13 //客户端登录返回
|
|
|
};
|
|
|
|
|
|
function set_duration () {
|
|
|
var video = $('.video_player').find('video')[0];
|
|
|
var duration;
|
|
|
var durationH;
|
|
|
var durationM;
|
|
|
var durationS;
|
|
|
|
|
|
if (video) {
|
|
|
video.addEventListener('loadedmetadata', function() {
|
|
|
if (this.duration) {
|
|
|
duration = Math.floor(this.duration);
|
|
|
durationH = Math.floor(duration / 3600);
|
|
|
durationM = Math.floor(duration / 60);
|
|
|
durationS = duration % 60;
|
|
|
|
|
|
duration = [durationH, durationM, durationS].map(function(val) {
|
|
|
if (val < 10) {
|
|
|
val = '0' + val;
|
|
|
} else {
|
|
|
val = '' + val;
|
|
|
}
|
|
|
|
|
|
return val;
|
|
|
}).join(':');
|
|
|
|
|
|
$('.duration .val').text(duration);
|
|
|
}
|
|
|
})
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 播放按钮
|
|
|
*/
|
|
|
function init_play_button() {
|
|
|
global.$btn_play = $btn_play = $('.live-video-play-button a');
|
|
|
var video_source = $('#video_container').attr('data-video');
|
|
|
$btn_play.on('click', function() {
|
|
|
$('#live-watermark').removeClass('hide');
|
|
|
if (live_type === 1 || live_type === 3) {
|
|
|
if ($('#' + video_id)[0] == undefined) {
|
|
|
get_chat_info(); //获取弹幕信息
|
|
|
|
|
|
var _is_ios = true;
|
|
|
var _is_wechat = false;
|
|
|
var agent = navigator.userAgent.toLowerCase();
|
|
|
//判断环境
|
|
|
if (agent.match(/android/i) == 'android') {
|
|
|
_is_ios = false;
|
|
|
}
|
|
|
|
|
|
if (agent.match(/MicroMessenger/i)) {
|
|
|
_is_wechat = true;
|
|
|
}
|
|
|
if (agent.match(/Weibo/i)) {
|
|
|
_is_wechat = true;
|
|
|
}
|
|
|
|
|
|
$(document).scrollTop(0);
|
|
|
$(this).parent().hide();
|
|
|
$('.live-video-cover').hide(); //隐藏封面
|
|
|
$('.live-app-download-head-wrap').hide(); //隐藏头部
|
|
|
|
|
|
var temp_height = $(window).height();
|
|
|
var temp_width = $(document).width();
|
|
|
if (temp_width > 540) {
|
|
|
temp_width = 540;
|
|
|
}
|
|
|
var scale = 17.1 / 20;
|
|
|
// if (temp_height > 30.15 * scale * (temp_width / 320 * 20)) {
|
|
|
// temp_height = 30.15 * scale + 'rem';
|
|
|
// } else {
|
|
|
// temp_height = temp_height + 'px'
|
|
|
// }
|
|
|
|
|
|
var v_width = temp_width + 'px';
|
|
|
var v_height = 32.575 * scale + 'rem';
|
|
|
$('.live-loading-container').show(); //显示loading
|
|
|
init_video(video_source, _is_wechat, _is_ios, '100%', '100%');
|
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
//$('#' + video_id)[0].play();
|
|
|
$('.live-status').show();
|
|
|
//interval = setInterval(test, 1000);// 测试弹幕
|
|
|
//interval1 = setInterval(test_like, 50);
|
|
|
}, 500);
|
|
|
|
|
|
$('.live-video-main').animate({
|
|
|
height: temp_height
|
|
|
}, 500, function() {
|
|
|
if (live_type == 3) {
|
|
|
$('.live-app-open').css({
|
|
|
'width': '100%',
|
|
|
'bottom': 0,
|
|
|
'left': 0,
|
|
|
'margin-left': 0,
|
|
|
'-webkit-border-radius': 0,
|
|
|
'border-radius': 0
|
|
|
});
|
|
|
$('.white-space').append($('.live-app-open'));
|
|
|
$('#live_touch_layer').hide();
|
|
|
$('.live-like-pannel').show();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
//console.log($('#' + video_id)[0],'click_2');
|
|
|
$(document).scrollTop(0);
|
|
|
$(this).parent().hide();
|
|
|
$('.live-loading-container').hide(); //隐藏loading
|
|
|
$('.live-video-cover').hide(); //隐藏封面
|
|
|
$('.live-app-download-head-wrap').hide(); //隐藏头部
|
|
|
if (live_type != 3) {
|
|
|
$('#' + video_id)[0].play();
|
|
|
} else {
|
|
|
//alert('click');
|
|
|
// video.pause();
|
|
|
$('#live_touch_layer').hide();
|
|
|
$('#' + video_id)[0].play();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} else {
|
|
|
$('.live-end').show();
|
|
|
}
|
|
|
|
|
|
if (typeof get_live_data == 'function') {
|
|
|
get_live_data(3, live_type); //数据统计
|
|
|
}
|
|
|
|
|
|
if (live_type === 3) {
|
|
|
set_duration();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function init_video(video_source, is_wechat, is_ios, width, height) { //初始化视频
|
|
|
if (is_live) {
|
|
|
$('.video_player').yohovideo({
|
|
|
video_id: video_id,
|
|
|
islive: is_live,
|
|
|
width: width,
|
|
|
height: height,
|
|
|
preload: 'auto', //'auto'当页面加载后载入视频,'meta'当页面加载后载入元数据,'none'页面加载后不载入
|
|
|
autoplay: false, //'autoplay'自动播放视频,false 不自动播放
|
|
|
controls: 'controls', //'controls'显示控件,false 不显示控件
|
|
|
poster: '', //封面图片地址,不填则不显示
|
|
|
loop: false, //'loop'播放结束后自动重播,false 不重播
|
|
|
src: video_source, //需要播放的视频地址
|
|
|
loading_div: '.live-loading-container', //loading的div
|
|
|
is_weixin: is_wechat,
|
|
|
is_ios: is_ios
|
|
|
});
|
|
|
} else {
|
|
|
$('.video_player').yohovideo({
|
|
|
video_id: video_id,
|
|
|
islive: is_live,
|
|
|
width: width,
|
|
|
height: height,
|
|
|
preload: 'auto', //'auto'当页面加载后载入视频,'meta'当页面加载后载入元数据,'none'页面加载后不载入
|
|
|
autoplay: false, //'autoplay'自动播放视频,false 不自动播放
|
|
|
controls: 'controls', //'controls'显示控件,false 不显示控件
|
|
|
poster: '', //封面图片地址,不填则不显示
|
|
|
loop: false, //'loop'播放结束后自动重播,false 不重播
|
|
|
src: video_source, //需要播放的视频地址
|
|
|
loading_div: '.live-loading-container', //loading的div
|
|
|
is_weixin: is_wechat,
|
|
|
is_ios: is_ios,
|
|
|
callback: replay_barrage_callback_handler
|
|
|
});
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化并试图连接弹幕服务器
|
|
|
*/
|
|
|
function get_chat_info() {
|
|
|
/*query_arr = ManageQueryString();
|
|
|
is_live = parseInt(getQueryString(query_arr, 'islive'));//判断是否是直播:0重播,1直播
|
|
|
if (is_live == undefined || isNaN(is_live)) {
|
|
|
is_live = true;
|
|
|
}
|
|
|
else {
|
|
|
is_live = Boolean(parseInt(is_live));
|
|
|
}*/
|
|
|
|
|
|
link_type = 'websocket';
|
|
|
|
|
|
if (is_live) {
|
|
|
get_websocket_server(link_type);
|
|
|
} else {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取弹幕服务器
|
|
|
* @param type 参数类型:websocket,tcp,用于获取不同协议的服务器地址
|
|
|
*/
|
|
|
function get_websocket_server(type) {
|
|
|
var param = type;
|
|
|
$.ajax({
|
|
|
url: get_socket_server_url,
|
|
|
data: {
|
|
|
type: type
|
|
|
},
|
|
|
type: 'GET',
|
|
|
dataType: 'json',
|
|
|
success: function(data) {
|
|
|
console.log(data);
|
|
|
if (data.code === 200) {
|
|
|
var arr = data.data[0].split(':');
|
|
|
var ip = arr[0];
|
|
|
var port = arr[1];
|
|
|
|
|
|
if (is_live) {
|
|
|
ip = 'ws://' + ip;
|
|
|
link_to_websocket_server(ip, port);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 连接弹幕服务器
|
|
|
* @param ip
|
|
|
* @param port
|
|
|
*/
|
|
|
function link_to_websocket_server(ip, port) {
|
|
|
var path = ip + ':' + port;
|
|
|
live_ws = new WebSocket(path);
|
|
|
|
|
|
live_ws.onopen = function(event) {
|
|
|
live_ws.send(get_cmd(CMD.LOGIN));
|
|
|
heart_beat_interval = setInterval(send_heart_beat, 60000);
|
|
|
send_heart_beat();
|
|
|
};
|
|
|
|
|
|
live_ws.onerror = function(event) {
|
|
|
console.log(event);
|
|
|
live_ws.close();
|
|
|
live_ws = null;
|
|
|
clearInterval(heart_beat_interval);
|
|
|
link_to_websocket_server(ip, port);
|
|
|
};
|
|
|
|
|
|
live_ws.onclose = function(event) {
|
|
|
console.log(event);
|
|
|
clearInterval(heart_beat_interval);
|
|
|
};
|
|
|
|
|
|
live_ws.onmessage = function(event) {
|
|
|
if (event.data) {
|
|
|
var msg_obj = JSON.parse(event.data);
|
|
|
console.log(msg_obj, msg_obj.cmd);
|
|
|
switch (msg_obj.cmd) {
|
|
|
case 4:
|
|
|
insert_user(msg_obj);
|
|
|
break;
|
|
|
case 5:
|
|
|
insert_msg(msg_obj);
|
|
|
break;
|
|
|
case 9:
|
|
|
insert_like(msg_obj);
|
|
|
break;
|
|
|
case 10:
|
|
|
insert_audience(msg_obj);
|
|
|
break;
|
|
|
case 12:
|
|
|
insert_end(msg_obj);
|
|
|
break;
|
|
|
case 13:
|
|
|
receive_init(msg_obj);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取重播弹幕
|
|
|
* @param start_time 需要从什么时间开始获取
|
|
|
* @param duration 获取多少秒时间的内容
|
|
|
*/
|
|
|
|
|
|
function get_replay_chat_barrage(start_time, duration) {
|
|
|
if (replay_new_start_time === 0) {
|
|
|
replay_new_start_time = start_time;
|
|
|
}
|
|
|
is_replay_chat_loading = true;
|
|
|
var ajax = $.ajax({
|
|
|
url: get_replay_chat_url,
|
|
|
data: {
|
|
|
id: live_room,
|
|
|
startTime: start_time,
|
|
|
timeInterval: duration
|
|
|
},
|
|
|
type: 'GET',
|
|
|
dataType: 'json',
|
|
|
timeout: 5000,
|
|
|
success: function(data) {
|
|
|
console.log('load_replay_data=', data);
|
|
|
if (data.code === 200) {
|
|
|
if (data.data.length > 0) {
|
|
|
data.data.forEach(function(obj) {
|
|
|
replay_chat_arr.push(obj);
|
|
|
});
|
|
|
if (replay_chat_frame_count == 0) {
|
|
|
set_replay_chat_frame();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
setTimeout(function() { //setTimeout的理由是防止requestAnimationFrame在调用的那一秒内重复读取
|
|
|
is_replay_chat_loading = false;
|
|
|
}, 1000);
|
|
|
|
|
|
},
|
|
|
complete: function(XMLHttpRequest, status) {
|
|
|
if (status == 'timeout') { //超时
|
|
|
setTimeout(function() {
|
|
|
is_replay_chat_loading = false;
|
|
|
}, 1000);
|
|
|
ajax.abort();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
//用于测试
|
|
|
/* var test_arr = test_replay(start_time, duration);
|
|
|
test_arr.forEach(function (obj) {
|
|
|
replay_chat_arr.push(obj);
|
|
|
});
|
|
|
if (replay_chat_frame_count == 0) {
|
|
|
set_replay_chat_frame();
|
|
|
}
|
|
|
setTimeout(function () {
|
|
|
is_replay_chat_loading = false;
|
|
|
}, 1000);*/
|
|
|
}
|
|
|
/**
|
|
|
* 开始
|
|
|
*/
|
|
|
function set_replay_chat_frame() {
|
|
|
var element = $('#live_chat_ul');
|
|
|
requestAnimationFrame(manage_replay_chat_msg);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 重播时弹幕处理
|
|
|
*/
|
|
|
function manage_replay_chat_msg() {
|
|
|
replay_chat_frame_count += 1;
|
|
|
|
|
|
if (replay_video != undefined) {
|
|
|
if (!replay_video.paused && !is_wating) {
|
|
|
var time = parseInt(replay_video.currentTime);
|
|
|
if (!isNaN(replay_video.duration) && replay_video.duration != 0) {
|
|
|
if (replay_video_duration === 0) {
|
|
|
replay_video_duration = replay_video.duration;
|
|
|
replay_per_like = replay_total_likes / (replay_video_duration - 10);
|
|
|
replay_per_request_like = replay_total_likes / (replay_video_duration - 10) / 60;
|
|
|
}
|
|
|
|
|
|
if (replay_video_duration > 0 && replay_now_like < replay_total_likes && replay_video.currentTime >= 10) {
|
|
|
if (replay_per_request_like > 1) {
|
|
|
replay_now_like += 1;
|
|
|
} else {
|
|
|
replay_now_like += replay_per_request_like;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (replay_now_like - now_like_nums >= 1) {
|
|
|
now_like_nums = parseInt(replay_now_like);
|
|
|
var msg = '{"cmd":9,"msg":' + now_like_nums + ',"room":' + live_room + ',"uid":1}';
|
|
|
var msg_obj = JSON.parse(msg);
|
|
|
insert_like(msg_obj);
|
|
|
}
|
|
|
} else if (replay_video.currentTime < 10) {
|
|
|
var msg = '{"cmd":9,"msg":0,"room":' + live_room + ',"uid":1}';
|
|
|
var msg_obj = JSON.parse(msg);
|
|
|
insert_like(msg_obj);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (replay_chat_arr.length > 0) {
|
|
|
var obj = replay_chat_arr[0];
|
|
|
if (obj.create_time <= time) {
|
|
|
if (obj.cmd == 5) {
|
|
|
insert_msg(obj)
|
|
|
}
|
|
|
if (obj.cmd == 4) {
|
|
|
insert_user(obj);
|
|
|
}
|
|
|
replay_chat_arr.splice(obj, 1);
|
|
|
}
|
|
|
}
|
|
|
//如果当前播放时间+开始时间 = 新的搜索播放时间 + 读取的时间区间 - 10秒。那么开始读取新的数据
|
|
|
//因为存在video的seek,seek之后replay_new_start_time会变化,需要从这个时间往后加55秒再读取新数据(ios7以下为5秒),防止短时间重复读取出错
|
|
|
if (time + live_start_time == replay_new_start_time + replay_chat_interval - 5) {
|
|
|
if (!is_replay_chat_loading) {
|
|
|
replay_new_start_time += replay_chat_interval;
|
|
|
get_replay_chat_barrage(replay_new_start_time, replay_chat_interval);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
replay_video = $('#' + video_id)[0];
|
|
|
}
|
|
|
requestAnimationFrame(manage_replay_chat_msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* seeked之后触发的重新读取事件
|
|
|
* s
|
|
|
* @param time
|
|
|
*/
|
|
|
function replay_barrage_callback_handler(time, v_type) {
|
|
|
switch (v_type) {
|
|
|
case 'seeked':
|
|
|
console.log('seeked');
|
|
|
replay_chat_arr = [];
|
|
|
$('#live_chat_ul').html('');
|
|
|
replay_new_start_time = parseInt(time) + live_start_time;
|
|
|
get_replay_chat_barrage(replay_new_start_time, replay_chat_interval);
|
|
|
break;
|
|
|
case 'timeupdate':
|
|
|
//部分机型或系统版本无法使用seek,所以把读取时间缩短到10秒一次,并且判断当前播放时间与存留的搜索时间replay_new_start_time之间的
|
|
|
//差值,大于replay_chat_interval或小于-replay_chat_interval的则被认定为拖动了进度条。
|
|
|
if (!can_seek) {
|
|
|
if ((parseInt(time) + live_start_time - replay_new_start_time > replay_chat_interval) || (replay_new_start_time - parseInt(time) - live_start_time > replay_chat_interval)) {
|
|
|
console.log('大于或小雨');
|
|
|
replay_new_start_time = parseInt(time) + live_start_time;
|
|
|
replay_chat_arr = [];
|
|
|
$('#live_chat_ul').html('');
|
|
|
get_replay_chat_barrage(replay_new_start_time, replay_chat_interval);
|
|
|
}
|
|
|
}
|
|
|
if (parseInt(time) > 10) {
|
|
|
replay_now_like = (parseInt(time) - 10) * replay_per_like;
|
|
|
if (replay_now_like - now_like_nums <= -30) { //回退的时候让当前显示的点赞数和重播点赞数一致
|
|
|
console.log('<');
|
|
|
now_like_nums = replay_now_like;
|
|
|
}
|
|
|
} else {
|
|
|
replay_now_like = 0;
|
|
|
now_like_nums = 0;
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
case 'waiting':
|
|
|
is_wating = true;
|
|
|
break;
|
|
|
case 'canplaythrough':
|
|
|
is_wating = false;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回命令json字符串
|
|
|
* @param cmd
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
function get_cmd(cmd) {
|
|
|
var result = '';
|
|
|
switch (cmd) {
|
|
|
case 1:
|
|
|
result = '{"cmd":' + cmd + ',"uid":"","name":"","avatar":"","room":' + live_room + '}';
|
|
|
break;
|
|
|
case 7:
|
|
|
result = '{"cmd":' + cmd + ',"msg":"heart_html5","room":' + live_room + '}';
|
|
|
break;
|
|
|
}
|
|
|
console.log(result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 每隔一分钟向服务器发送一次心跳包
|
|
|
*/
|
|
|
function send_heart_beat() {
|
|
|
if (live_ws) {
|
|
|
live_ws.send(get_cmd(CMD.HEART_BEAT));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 插入用户新消息
|
|
|
* @param obj
|
|
|
*/
|
|
|
|
|
|
function insert_msg(obj) {
|
|
|
var $ul = $('#live_chat_ul');
|
|
|
var msg_html
|
|
|
if (obj.avatar == '' || obj.avatar == undefined) {
|
|
|
var rand = Math.floor(Math.random() * 5) + 1;
|
|
|
// obj.avatar = site_url + '/img/activity/live/live/head_' + rand + '.png';
|
|
|
|
|
|
msg_html = '<div class="live-item2"><div class="live-item2-head">' +
|
|
|
'<img class="avatar' + rand + '"></div>' +
|
|
|
'<div class="live-item2_1">' +
|
|
|
'<span class="live-replay">@' + obj.name + '</span>'+
|
|
|
'<div class="live-item2_2">' +
|
|
|
obj.msg +
|
|
|
'</div>' +
|
|
|
'</div>' +
|
|
|
'</div>';
|
|
|
} else {
|
|
|
obj.avatar = obj.avatar.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
|
|
|
const dict = {
|
|
|
'{width}': 50,
|
|
|
'{height}': 50,
|
|
|
'{mode}': 2
|
|
|
};
|
|
|
|
|
|
return dict[$0];
|
|
|
})
|
|
|
|
|
|
msg_html = '<div class="live-item2"><div class="live-item2-head">' +
|
|
|
'<img src="' + obj.avatar + '"></div>' +
|
|
|
'<div class="live-item2_1">' +
|
|
|
'<span class="live-replay">@' + obj.name + '</span>'+
|
|
|
'<div class="live-item2_2">' +
|
|
|
obj.msg +
|
|
|
'</div>' +
|
|
|
'</div>' +
|
|
|
'</div>';
|
|
|
}
|
|
|
var li = document.createElement('li');
|
|
|
li.innerHTML = msg_html;
|
|
|
if ($ul.children().length >= 4) {
|
|
|
$ul.find('li').eq(0).remove();
|
|
|
}
|
|
|
$ul.append(li);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 插入新用户加入
|
|
|
* @param obj
|
|
|
*/
|
|
|
function insert_user(obj) {
|
|
|
if (obj.name.length > 1) {
|
|
|
var $ul = $('#live_chat_ul');
|
|
|
var msg_html = '<div class="live-item1">' + '@' + obj.name + ' <span>已加入</span>' + '</div>';
|
|
|
var li = document.createElement('li');
|
|
|
li.innerHTML = msg_html;
|
|
|
if ($ul.children().length >= 4) {
|
|
|
$ul.find('li').eq(0).remove();
|
|
|
|
|
|
}
|
|
|
$ul.append(li);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户点赞
|
|
|
* @param obj
|
|
|
*/
|
|
|
function insert_like(obj) {
|
|
|
var num = obj.msg.toString();
|
|
|
|
|
|
if (num.indexOf('万') < 0) {
|
|
|
num = parseInt(num);
|
|
|
if (isNaN(num)) {
|
|
|
num = 0;
|
|
|
}
|
|
|
}
|
|
|
if (now_like_nums == 0) {
|
|
|
$('.live-like-pannel').show();
|
|
|
} else {
|
|
|
if (!is_animate) {
|
|
|
is_animate = true;
|
|
|
var like_rand = Math.floor(Math.random() * 3) + 1;
|
|
|
var img_rand = Math.floor(Math.random() * 5) + 1;
|
|
|
var img_path = site_url + '/img/activity/live/live/like_icon' + img_rand + '.png';
|
|
|
var id = 'js_keyframes_' + Math.random();
|
|
|
var like_class = 'like-icon' + ' scroll_animate_' + like_rand;
|
|
|
var msg_html = '<img src="' + img_path + '">';
|
|
|
|
|
|
|
|
|
var anim_div = document.createElement('div');
|
|
|
anim_div.id = id;
|
|
|
anim_div.className = like_class;
|
|
|
anim_div.innerHTML = msg_html;
|
|
|
anim_div.addEventListener('webkitAnimationEnd', function() {
|
|
|
$(this).remove();
|
|
|
});
|
|
|
|
|
|
$live_like_pannel.append(anim_div);
|
|
|
setTimeout(function() {
|
|
|
is_animate = false;
|
|
|
}, 40);
|
|
|
}
|
|
|
}
|
|
|
now_like_nums = num;
|
|
|
if (now_like_nums > 100000) {
|
|
|
$('#like_num').text((now_like_nums / 10000).toFixed(1) + '万');
|
|
|
} else {
|
|
|
$('#like_num').text(now_like_nums);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送心跳包之后返回的在线人数
|
|
|
* @param msg_obj
|
|
|
*/
|
|
|
function insert_audience(obj) {
|
|
|
$('.live-num span').text(obj.onlineNums + '人观看');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通知直播结束
|
|
|
* @param obj
|
|
|
*/
|
|
|
function insert_end(obj) {
|
|
|
var user_num = obj.audienceNums.toString();
|
|
|
var like_num = obj.likeNums.toString();
|
|
|
var video_len = obj.videoLen.toString();
|
|
|
|
|
|
if (user_num.indexOf('万') < 0) {
|
|
|
user_num = parseInt(user_num);
|
|
|
}
|
|
|
|
|
|
if (like_num.indexOf('万') < 0) {
|
|
|
like_num = parseInt(like_num);
|
|
|
}
|
|
|
|
|
|
if (user_num > 10000) {
|
|
|
user_num = (user_num / 10000).toFixed(1) + '万';
|
|
|
}
|
|
|
|
|
|
if (like_num > 10000) {
|
|
|
like_num = (like_num / 10000).toFixed(1) + '万';
|
|
|
}
|
|
|
|
|
|
var video = $('#' + video_id)[0];
|
|
|
if (video) {
|
|
|
setTimeout(function() {
|
|
|
video.pause();
|
|
|
}, 1000);
|
|
|
live_type = 2;
|
|
|
}
|
|
|
live_ws.close();
|
|
|
clearInterval(live_time_interval);
|
|
|
var $liveEnd = $('#live-state-end');
|
|
|
$liveEnd.show();
|
|
|
$liveEnd.find('.audience .val').text(user_num);
|
|
|
$liveEnd.find('.duration .val').text(video_len);
|
|
|
$liveEnd.find('.favorite .val').text(like_num);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 登录之后服务端返回当前直播人数及点赞数
|
|
|
* @param obj
|
|
|
*/
|
|
|
function receive_init(obj) {
|
|
|
$('.live-num span').text(obj.onlineNums + '人观看');
|
|
|
now_like_nums = parseInt(obj.likeNums);
|
|
|
if (now_like_nums > 100000) {
|
|
|
$('#like_num').text((now_like_nums / 10000).toFixed(1) + '万');
|
|
|
} else {
|
|
|
$('#like_num').text(now_like_nums);
|
|
|
}
|
|
|
$('.live-like-pannel').show();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取已直播时间
|
|
|
*/
|
|
|
function get_live_time() {
|
|
|
if (live_start_time) {
|
|
|
var date = new Date();
|
|
|
live_duration = parseInt(date.getTime() / 1000) - live_start_time;
|
|
|
console.log('live_duration=' + live_duration);
|
|
|
$('#live_time').text(get_time_text(live_duration));
|
|
|
live_time_interval = setInterval(set_interval_time, 1000);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回时间字符串00:00
|
|
|
* @param time
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
function get_time_text(time) {
|
|
|
var time_text = '00:00:00';
|
|
|
var sec;
|
|
|
var min;
|
|
|
var hour;
|
|
|
if (time < 0) {
|
|
|
time = 0;
|
|
|
}
|
|
|
|
|
|
if (time < 10) {
|
|
|
min = '00';
|
|
|
sec = '0' + time;
|
|
|
hour = '';
|
|
|
} else if (time >= 60 && time < 3600) {
|
|
|
min = (time - time % 60) / 60;
|
|
|
if (min < 10) {
|
|
|
min = '0' + min;
|
|
|
}
|
|
|
sec = time % 60;
|
|
|
if (sec < 10) {
|
|
|
sec = '0' + sec;
|
|
|
}
|
|
|
hour = '';
|
|
|
} else if (time >= 3600) {
|
|
|
hour = (time - time % 3600) / 3600;
|
|
|
if (hour < 10) {
|
|
|
hour = '0' + hour + ':';
|
|
|
} else {
|
|
|
hour = hour + ':';
|
|
|
}
|
|
|
var rests = time % 3600;
|
|
|
min = (rests - rests % 60) / 60;
|
|
|
if (min < 10) {
|
|
|
min = '0' + min;
|
|
|
}
|
|
|
|
|
|
sec = rests % 60;
|
|
|
if (sec < 10) {
|
|
|
sec = '0' + sec;
|
|
|
}
|
|
|
} else {
|
|
|
min = '00';
|
|
|
sec = time;
|
|
|
hour = '';
|
|
|
}
|
|
|
time_text = hour + min + ':' + sec;
|
|
|
return time_text;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 直播时间每秒+1
|
|
|
*/
|
|
|
function set_interval_time() {
|
|
|
live_duration += 1;
|
|
|
get_time_text(live_duration);
|
|
|
$('#live_time').text(get_time_text(live_duration));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* requestAnimationFrame
|
|
|
*/
|
|
|
function init_request_animation_frames() {
|
|
|
var lastTime = 0;
|
|
|
var prefix;
|
|
|
//通过遍历各浏览器前缀,来得到requestAnimationFrame和cancelAnimationFrame在当前浏览器的实现形式
|
|
|
for (var i = 0; i < prefixes.length; i++) {
|
|
|
if (requestAnimationFrame && cancelAnimationFrame) {
|
|
|
break;
|
|
|
}
|
|
|
prefix = prefixes[i];
|
|
|
requestAnimationFrame = requestAnimationFrame || window[prefix + 'RequestAnimationFrame'];
|
|
|
cancelAnimationFrame = cancelAnimationFrame || window[prefix + 'CancelAnimationFrame'] || window[prefix + 'CancelRequestAnimationFrame'];
|
|
|
}
|
|
|
|
|
|
//如果当前浏览器不支持requestAnimationFrame和cancelAnimationFrame,则会退到setTimeout
|
|
|
if (!requestAnimationFrame || !cancelAnimationFrame) {
|
|
|
requestAnimationFrame = function(callback) {
|
|
|
var currTime = new Date().getTime();
|
|
|
//为了使setTimteout的尽可能的接近每秒60帧的效果
|
|
|
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
|
|
|
var id = window.setTimeout(function() {
|
|
|
callback(currTime + timeToCall);
|
|
|
}, timeToCall);
|
|
|
lastTime = currTime + timeToCall;
|
|
|
return id;
|
|
|
};
|
|
|
|
|
|
cancelAnimationFrame = function(id) {
|
|
|
window.clearTimeout(id);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
//得到兼容各浏览器的API
|
|
|
window.requestAnimationFrame = requestAnimationFrame;
|
|
|
window.cancelAnimationFrame = cancelAnimationFrame;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 入口
|
|
|
*/
|
|
|
$(document).ready(function() {
|
|
|
|
|
|
init_request_animation_frames();
|
|
|
if (check_supperUniversalLink() != undefined && check_supperUniversalLink() <= 7) {
|
|
|
can_seek = false;
|
|
|
replay_chat_interval = 10;
|
|
|
}
|
|
|
|
|
|
$('.live-app-open a').on('click', function() {
|
|
|
check_sys_open(); //live_head.js中的方法
|
|
|
});
|
|
|
init_play_button();
|
|
|
if (live_type != 3) { //直播的时间
|
|
|
is_live = true;
|
|
|
get_live_time();
|
|
|
|
|
|
} else {
|
|
|
is_live = false;
|
|
|
// $('.live-time span').eq(0).text('YOHO!回看 ·');
|
|
|
$('.live-num span').text(replay_user_nums + '人观看');
|
|
|
replay_new_start_time = live_start_time;
|
|
|
get_replay_chat_barrage(live_start_time, replay_chat_interval); //重播获取弹幕
|
|
|
|
|
|
}
|
|
|
|
|
|
$live_like_pannel = $('#live_like_pannel');
|
|
|
});
|
|
|
|
|
|
global.test = function test() { //测试弹幕样式
|
|
|
var rand = Math.floor(Math.random() * msg_obj.length);
|
|
|
var obj = JSON.parse(msg_obj[rand].toString());
|
|
|
switch (obj.cmd) {
|
|
|
case 4:
|
|
|
insert_user(obj);
|
|
|
break;
|
|
|
case 5:
|
|
|
insert_msg(obj);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
global.test_like = function test_like() {
|
|
|
var rand = Math.floor(Math.random() * 5);
|
|
|
test_count += rand;
|
|
|
var obj_text = '{"cmd":9,"msg":' + test_count + ',"room":' + live_room + ',"uid":0}';
|
|
|
var obj = JSON.parse(obj_text);
|
|
|
insert_like(obj);
|
|
|
}
|
|
|
|
|
|
global.test_replay = function test_replay(starttime, timeinterval) {
|
|
|
var start = starttime - live_start_time;
|
|
|
var end = start + timeinterval;
|
|
|
var test_arr = [];
|
|
|
if (replay_msg.length > 0) {
|
|
|
for (var i = 0; i < replay_msg.length; i++) {
|
|
|
var obj = replay_msg[i];
|
|
|
var create_time = parseInt(obj.createtime);
|
|
|
if (create_time >= start && create_time <= end) {
|
|
|
test_arr.push(obj);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return test_arr;
|
|
|
}
|
|
|
|
|
|
module.exports = {}; |
...
|
...
|
|