一、问题背景:
在最近的一个项目中,需要有一个上传视频的功能,原先使用的优酷开放平台,但是由于很久没有维护了,导致账号过期了,且优酷开放平台在17年三月份之后,就暂停新应用的创建和生成新的应用,所以重新选用了爱奇艺开放平台。
二、使用步骤
(说明:申请账号,创建应用和审核,请参考官方的文档,我在这里就不做详细的说明了,这里直接上代码。不过我也是第一次使用这个爱奇艺的开放平台,所以可能会有错误,但是功能是可以实现的)
引用sdkbase_min.js
HTML:
JS代码
/*解决input中placeholder值在ie中无法支持的问题*/ $(document).ready(function(){ var doc=document,inputs=doc.getElementsByTagName('input'),supportPlaceholder='placeholder'in doc.createElement('input'),placeholder=function(input){var text=input.getAttribute('placeholder'),defaultValue=input.defaultValue; if(defaultValue==''){ input.value=text} input.οnfοcus=function(){ if(input.value===text){this.value=''}}; input.οnblur=function(){if(input.value===''){this.value=text}}}; if(!supportPlaceholder){ for(var i=0,len=inputs.length;ipersonal/uploadVideo?videoId='+fileinfo.file_id ; }, onError:function () { alert("上传失败"); } }); }else{ alert("上传成功"); } setTimeout(function () { uoploader.delLocal(fileinfo.file_name,fileinfo.file_id); // 20141227 resetPer(); }, 2000); }, onError:function (data) { $("#btn-upload-start").attr("disabled",false); if(data.msg){ // 续传 if(data.msg=='network break down'){ breakdown=true; uoploader.pauseUpload(); } } else{ alert("上传失败"); } }, onProgress:function (data) { // 5/7 增加速度,剩余时间 var per = document.getElementById("percent"); per.style.width = data.percent + "%"; $("#percent_text").text("上传中....速度:"+data.speed+"kb/s , 剩余时间:"+data.remainTime + "s"); } }); }); }); function checkTitle(){ //显示数据 $("#title_error_span").hide(); $("#title_right_span").hide(); //获取昵称 var title = $("#input01").val(); //判断用户名是否为空 if(title == "" || title == null){ $("#title_right_span").hide(); $("#title_error_span").show(); $("#btn-upload-start").attr("disabled","disabled"); }else if(title.length>30){ $("#title_error_text").text("标题最多能输入30个字"); $("#title_right_span").hide(); $("#title_error_span").show(); $("#btn-upload-start").attr("disabled","disabled"); }else{ $("#title_error_span").hide(); $("#title_right_span").show(); $("#btn-upload-start").attr("disabled",false); } } function checkTags(){ var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]"); //显示数据 $("#tags_error_span").hide(); $("#tags_right_span").hide(); var tags = $("#input02").val(); //判断用户名是否为空 if(tags == "" || tags == null){ $("#tags_right_span").hide(); $("#tags_error_span").show(); $("#btn-upload-start").attr("disabled","disabled"); }else if(tags.length<2 || tags.length>12){ $("#tags_right_span").hide(); $("#tags_error_span").show(); $("#tags_error_text").text("标签内容限定2~12的字符"); $("#btn-upload-start").attr("disabled","disabled"); } //特殊字符正则表达式 else if (txt.test(tags)){ $("#tags_right_span").hide(); $("#tags_error_span").show(); $("#tags_error_text").text("标签含有特殊字符"); $("#btn-upload-start").attr("disabled","disabled"); }else{ $("#tags_error_span").hide(); $("#tags_right_span").show(); $("#btn-upload-start").attr("disabled",false); } } /** * 检查简介 */ function checkDric(){ //显示数据 $("#description_error_span").hide(); $("#description_right_span").hide(); var content = $("#textarea").val().toString(); var len = 0; if (content != null && content != "") { for ( var j = 0; j < content.length; j++) { var str = content.charAt(j); var reg = /^[\u4E00-\u9FA5]+$/; if (reg.test(str)) { len += 2; } else { len += 1; } } } if(content == "" || len == 0 ){ $("#description_right_span").hide(); $("#description_error_text").text("请输入简介"); $("#description_error_span").show(); $("#btn-upload-start").attr("disabled","disabled"); } if(len <= 25 && len >0){ $("#description_error_span").hide(); $("#description_right_span").show(); $("#btn-upload-start").attr("disabled",false); }else{ $("#description_right_span").hide(); $("#description_error_span").show(); $("#btn-upload-start").attr("disabled","disabled"); } }
上传完成后,页面播放。JS代码
// 获取视频id var videoId = $("#videoId").attr("value"); // 初始化 var vcop = new Q.vcopClient({ appKey:"618c7aca5e6d47648e6c4d6fd2e246af", // 填写申请的app key appSecret:"13a3fbb37e707ec19322c0478d860e7c", // 填写app secret managerUrl:"http://openapi.iqiyi.com/", uploadUrl:"http://upload.iqiyi.com/", needMeta:false }); // 授权 vcop.getAuthEnterprise(function (data) { if(data){ vcop.authtoken = data.data.access_token; console.log(vcop.authtoken); // 获取视频信息 vcop.getVideoInfo({ file_ids:videoId // 获取视频信息 }, function (result) { console.log(result); console.log("1:"+result); console.log("2:"+(result.code == "A00000")); if(result.code == "A00000"){ var _r = result.data; console.log("3:"+_r) console.log("4:"+(_r != '[]')); if(_r != '[]' && _r != undefined && _r != '' && _r != null){ if(_r[0].fileStatus == 1){ // 发布中 imghtml = ""; $("#shipin_img_div").html(imghtml); }else if(_r[0].fileStatus == 2){ // 已经发布 $("#shiping_img_div").css("display","none"); // 这行代码主要是为了获取vid和tvid $.get("http://openapi.iqiyi.com/api/file/fullStatus?access_token="+vcop.authtoken+"&file_id="+videoId,function(data){ data = $.parseJSON(data); var swfUrl = data.data.swfurl; var vid = swfUrl.substring(swfUrl.indexOf("vid=") + 4, swfUrl.indexOf("&tvId")); var tvid = swfUrl.substring(swfUrl.indexOf("tvId=") + 5, swfUrl.indexOf("&cnId")); // 这行代码,copy open.iqiyi.com -->我的应用 -->播放器设置的那段代码 imghtml = "