// 前端js与webView的交互 var player = { callbackCode: null, //客户端操作返回的code targetSeekTime: null, //快进快退跳转的目标时间 seekTimeout: null, //快进快退的防抖延时器 isAding: 0, //当前播放器播放广告的状态码(未来电视播放器) // 播放视频 play: function (obj) { // 设置参数 var playConfig = { type: obj.type ? obj.type : 1, //设置播放类型,默认为1 url: obj.url ? obj.url : '', //通过url播放,type为1时必须填 code: obj.code ? obj.code : '', //通过code播放,type为2时必须填 seek_time: obj.seek_time ? obj.seek_time * 1000 : 0, //设置起始时间,默认为0,传入参数单位为秒 x: obj.x ? obj.x : 0, //播放器位置,左(右)上角的x轴坐标点,默认为0 y: obj.y ? obj.y : 0, //播放器位置,左(右)上角的y轴坐标点,默认为0 width: obj.width ? obj.width : 1280, //播放器大小,宽度,默认为1280 height: obj.height ? obj.height : 720, //播放器大小,高度,默认为720 examine_id: obj.examine_id ? obj.examine_id : '', //未来电视专用审核ID; (对接未来电视播放的地区必填) examine_type: obj.examine_type ? obj.examine_type : '' //未来电视专用审核类型; (对接未来电视播放的地区必填) } // 播放类型为1时,url不能为空 if (playConfig.url == '' && playConfig.type == 1) { console.log('播放类型为1,请设置url'); return } // 播放类型为2是,code不能为空 else if (playConfig.code == '' && playConfig.type == 2) { console.log('播放类型为2,请设置code'); return } // 播放视频,并记录操作返回的code player.callbackCode = PlayerToJS.play(JSON.stringify(playConfig)); //code == 6, 执行成功 //code == -1, 执行过程中发生异常,中止执行 //code == 7, 参数有误,中止执行 //code == 8, 播放对象为空,中止执行 }, // 播放器尺寸改变 changePlayerSize: function (obj) { // 设置参数 var changeConfig = { x: obj.x ? obj.x : 0, y: obj.y ? obj.y : 0, width: obj.width ? obj.width : 1280, height: obj.height ? obj.height : 720, } // 更改播放器尺寸,并记录操作返回的code player.callbackCode = PlayerToJS.change(JSON.stringify(changeConfig)); //code == 6, 执行成功 //code == -1, 执行过程中发生异常,中止执行 //code == 7, 参数有误,中止执行 //code == 8, 播放对象为空,中止执行 // code == 9, 播放对象为初始化,中止执行 }, // 暂停播放 pause: function () { player.callbackCode = PlayerToJS.pause(); }, // 恢复播放 resume: function () { player.callbackCode = PlayerToJS.resume(); }, // 结束播放 stop: function () { player.callbackCode = PlayerToJS.stop(); }, // 快进(秒) forward: function (second) { var _this = this var durationTime = _this.getDurationTime() // 获取当前时长,若用户500毫秒内按过快进快退,则会获取到之前的目标时间 var currentTime = _this.targetSeekTime != null ? _this.targetSeekTime : _this.getCurrentTime() // 若没有传入参数,则默认设置跳跃时间为总时长的20分之1 var time = second ? second : durationTime * 0.05; // 计算要跳跃到的目标时间 var targetTime = Math.floor(currentTime + time) // 判断目标时间是否超越视频总时长,是则跳到倒数第二秒,否则跳到目标时间 if (targetTime >= durationTime) { targetTime = durationTime - 2 } // 记录下目标时间并调用防抖跳转方法 _this.targetSeekTime = targetTime _this.playTo() }, // 快退(秒) backward: function (second) { var _this = this var durationTime = _this.getDurationTime() // 获取当前时长,若用户500毫秒内按过快进快退,则会获取到之前的目标时间 var currentTime = _this.targetSeekTime != null ? _this.targetSeekTime : _this.getCurrentTime() $.Debuglog(currentTime) // 若没有传入参数,则默认设置跳跃时间为总时长的20分之1 var time = second ? second : durationTime * 0.05; // 计算要跳跃到的目标时间 var targetTime = Math.floor(currentTime - time) // 判断目标时间是否低于0,是则跳到倒数第二秒,否则跳到目标时间 if (targetTime <= 0) { targetTime = 0 } // 记录下目标时间并调用防抖跳转方法 _this.targetSeekTime = targetTime _this.playTo() }, // 进度跳转(秒→毫秒) seek: function (time) { var paramJson = { time : time * 1000 } player.callbackCode = PlayerToJS.seek(JSON.stringify(paramJson)); }, // 快进快退防抖跳转 playTo: function () { var _this = this // 设置500毫秒的防抖 clearTimeout(_this.seekTimeout) _this.seekTimeout = setTimeout(function() { // 500毫秒内没有快进快退操作,则正式跳转到目标时间,并清除目标时间 _this.seek(_this.targetSeekTime) _this.targetSeekTime = null }, 500) }, // 获取视频总时长(毫秒→秒) getDurationTime: function () { return PlayerToJS.getDuration() / 1000 }, // 获取视频当前时长(毫秒→秒) getCurrentTime: function () { var _this = this // 若快进快退的目标时间存在,则当前时长应为目标时长 var currentTime = _this.targetSeekTime ? _this.targetSeekTime : PlayerToJS.getCurrentPlayTime() / 1000 return currentTime }, // 获取视频当前进度(小数) getProgress: function () { var _this = this var durationTime = _this.getDurationTime() var currentTime = _this.getCurrentTime() return currentTime / durationTime }, // 获取视频当前进度(百分比字符串) getProgressString: function () { var _this = this var progress = _this.getProgress() return Number(progress * 100).toFixed(3) + '%' }, // 获取当前播放状态 getPlayerStatus: function () { return PlayerToJS.getPlayerStatus() // 1,视频准备中; // 2,播放; // 3,暂停; // 4,缓冲; // 5,播放完成停止播放; // 6,未播放; }, // VR视角旋转(仅限江苏移动VR大厅项目) rotate: function (x, y) { var rotateConfig = { x: x, y: y } player.callbackCode = PlayerToJS.rotate(JSON.stringify(rotateConfig)); }, // 判断当前播放器是否正在播放广告(仅限未来电视播放器) getCNTVPlayerAding: function () { var _this = this _this.isAding= PlayerToJS. getCNTVPlayerAding () } }