————————————2015-5-13————最新更新———————————————
javascript
因爲jwplayer6已經徹底沒法正常工做,如下介紹的方法也已經失效,請更換視頻播放插件,能夠參考樓主的文章:php
http://blog.csdn.net/snow_finland/article/details/45670683css
一、選用jwplayer6做爲前端播放器前端
四、php經過curl方式讀取的視頻流java
加載視頻盒子的部分js代碼ios
function load_video_player(width,height,fileid,type,title,size){ var player_w = width; var player_h = height; var isiphone; $.ajax({ type: 'POST', url: 'devicecontroller.php', data: 'islink='+islink, dataType:"JSON", async: false, success:function(data){ if(data.code == 1){ isiphone = data.data; }else{ isiphone = 0; } } }); var file_path = '/XXX/url?fileid='+fileid+'&type='+type+'&title='+title+'&size='+size+'&isiphone='+isiphone; var player_wrap = [ '<div id="J_video_container" class="video_container hide">', '<div class="video_box">', '</div>', '</div>' ].join(''); var player_box = [ '<div id="Jwplayer_box">Loading player...</div>' ].join(''); if($('#J_video_container').length==0){ $('#J_video_main').append(player_wrap); } $('#J_video_container').css('height',player_h+'px'); if($('#Jwplayer_box').length==0){ $('#J_video_container .video_box').append(player_box); } $('.video_box').css({'width':player_w+'px','height':player_h+'px'}); $('#J_video_container').show(); jwplayer("Jwplayer_box").setup({ 'width': player_w, 'height': player_h, 'controlbar': 'bottom', "autostart":"true", 'provider': 'video', 'type': 'mp4', 'file': file_path }); }
其中請求檢查設備和瀏覽器類型的devicecontroller.php代碼ajax
//json_encode中文亂碼問題修正 function arrayRecursive(&$array){ foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key]);//若是是數組就進行遞歸操做 } else { if(is_string($value)){ $temp1= addslashes($value); $array[$key]= urlencode($temp1);//若是是字符串就urlencode }else{ $array[$key] = $value; } } } } function JSON($result) { $array=$result; arrayRecursive($array);//先將類型爲字符串的數據進行 urlencode $json = json_encode($array);//再將數組轉成JSON return urldecode($json);//最後將JSON字符串進行urldecode } require_once("Mobile_Detect.php"); $detect = new Mobile_Detect(); if($detect->isiPhone() || $detect->isiPad()){ if($detect->isSafari()|| $detect->isGenericBrowser()){//iphone和ipad上的safari或者微信瀏覽器 $iphone = 1; }else{ $iphone = 0; } }else{ $iphone = 0; } $data['code']=1; $data['data']=$iphone; echo JSON($data);
加載視頻流的php算法
session_start(); $fileid = $_GET['fileid']; $isiphone = $_GET['isiphone']; $file_name = $_GET['title']; $file_type = $_GET['type']; $file_size = $_GET['size']; $cookie = $_SESSION['token']; //若是沒有文件名,設置帶擴展名的默認文件名 if(empty($file_name)){ $type = substr($file_type,0,5); if($type == "video"){ $type = substr($file_type,6); $file_name = "downfile.".$type; } } header("Content-type: ".$file_type); if(isset($_SERVER['HTTP_RANGE'])){ $http_range = $_SERVER['HTTP_RANGE']; $at = explode('=',$http_range); $at = explode('-',$at[1]); $start = $at[0]; if($at[1]==null){ $end = $file_size - 1; }else{ $end = $at[1]; } $length = $end - $start + 1; if($isiphone!=1){ $url = "/XXX/url/".$fileid; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, Array($cookie)); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header("Content-Description: File Transfer"); header("Content-Transfer-Encoding: binary"); Header("Content-type: ".$file_type); Header('Content-Disposition: attacurlment; filename="'.$file_name.'"'); Header("Content-Length: ".$file_size); ob_end_flush(); flush(); $result = curl_exec($curl); curl_close($curl); }else{ header("Accept-Ranges: 0-$length"); set_time_limit(0); header("Content-Range: bytes $start-$end/$file_size"); header("Content-Length: $length"); $url2 = "/XXX/url/".$fileid; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, Array($cookie)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在 curl_setopt($ch, CURLOPT_TIMEOUT, 600); $content = curl_exec($ch); curl_close($ch); $output = substr($content,$start,$length); echo $output; exit; } }else{ $url = "/XXX/url/".$fileid; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, Array($cookie)); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header("Content-Description: File Transfer"); header("Content-Transfer-Encoding: binary"); Header("Content-type: ".$file_type); Header('Content-Disposition: attacurlment; filename="'.$file_name.'"'); Header("Content-Length: ".$file_size); ob_end_flush(); flush(); $result = curl_exec($curl); curl_close($curl); }
補充說明:json
一、在js中請求判斷一次設備,並經過js傳遞給php讀取視頻流,而不是在php端讀取視頻流時每次判斷設備:
由於ios safari在第一次(會設定$_SERVER['HTTP_RANGE'])請求的時候,能判斷出是ios的safari設備數組
在第二次以及以後的請求時,沒法判斷出是ios的safari設備
二、判斷是不是ios的safari,在以後的文章中介紹