1. 首先在阿里云控制中心找到視頻點播,如果沒有開通就需要先開通服務,最好先購買存儲流量包和播放流量包,不然就會以播放流量收費比較貴。
- 添加加速域名,默認加速是上海地區(qū),在配置管理-存儲管理可以找到存儲地址
- 添加好域名就要配置安全策略,比例黑白名單,指定只能在哪個域名下播放,是否支持瀏覽器直接打開等一些防止視頻被別人下載,需要在url鑒權設置兩個key
4.Php代碼 獲取音視頻播放憑證
<?php
require 'aliyun-php-sdk-core/Config.php';
// 假定您的源碼文件和aliyun-php-sdk處于同一目錄
use vod\Request\V20170321 as vod;
class Aliyun {
function init_vod_client($accessKeyId, $accessKeySecret) {
$regionId = 'cn-shenzhen';
// 點播服務所在的Region,國內(nèi)請?zhí)頲n-shanghai,不要填寫別的區(qū)域
$profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
return new DefaultAcsClient($profile);
}
function get_play_info($client, $videoId) {
$request = new vod\GetPlayInfoRequest();
$request->setVideoId($videoId);
$regionId = 'cn-shenzhen';
$request->setAuthTimeout(3600*24);
// 播放地址過期時間(只有開啟了URL鑒權才生效),默認為3600秒,支持設置最小值為3600秒
$request->setAcceptFormat('JSON');
$arr=$client->getAcsResponse($request);
return $arr;
}
function testGetVideoPlayAuth($client,$videoId) {
$request = new vod\GetVideoPlayAuthRequest();
$regionId = 'cn-shenzhen';
$request->setAcceptFormat('JSON');
$request->setRegionId($regionId);
$request->setVideoId($videoId);
//視頻ID
$response = $client->getAcsResponse($request);
return $response;
}
}
?>
function getAuth($videoId){
vendor('Aliyun.Aliyun');
$video=new \Aliyun();//引入sdk
$accid='xxxxxxxxxx';
$secret="'xxxxxxxxxx'";
$res=$video->init_vod_client($accid,$secret);
$ress=$video->testGetVideoPlayAuth($res,$videoId);
$url=json_decode(json_encode($ress),true);
$str=$url['PlayAuth'];
return $str;
//return $resss['PlayAuth'];
}
或者也可以獲取播放地址
function getPlayUrl($videoId){
vendor('Aliyun.Aliyun');
$video=new \Aliyun();
$accid='xxxxxxxxxx'
$secret='xxxxxxxxxx';
$res=$video->init_vod_client($accid,$secret);
$ress=$video->get_play_info($res,$videoId);
$url=json_decode(json_encode($ress),true);
//$str=$url['PlayAuth'];
$str=$url['PlayInfoList']['PlayInfo'][0]['PlayURL'];
return $str;
//return $resss['PlayAuth'];
}
5.前端播放代碼
引入播放器
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.9.19/skins/default/aliplayer-min.css" />
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.19/aliplayer-min.js"></script>
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.19/aliplayer-h5-min.js"></script>
<div class="video prism-player" " id="J_prismPlayer" ></div>
<script>
var player = new Aliplayer({
id: 'J_prismPlayer',
width: '100%',
height:'500px',
autoplay: true,
vid : '{$videoId}',
//必選參數(shù)。音視頻ID。示例:1e067a2831b641db90d570b6480f****。
playauth : '{$PlayAuth}',
//必選參數(shù)。音視頻播放憑證。
},function(player){
console.log('The player is created.')
});
</script>
這里是通過音頻id 和播放憑證播放。還有直接獲取播放地址播放。
最麻煩的是m3u8流播放,暫未研究