1. 首先在阿里云控制中心找到視頻點(diǎn)播,如果沒(méi)有開通就需要先開通服務(wù),最好先購(gòu)買存儲(chǔ)流量包和播放流量包,不然就會(huì)以播放流量收費(fèi)比較貴。
- 添加加速域名,默認(rèn)加速是上海地區(qū),在配置管理-存儲(chǔ)管理可以找到存儲(chǔ)地址
- 添加好域名就要配置安全策略,比例黑白名單,指定只能在哪個(gè)域名下播放,是否支持瀏覽器直接打開等一些防止視頻被別人下載,需要在url鑒權(quán)設(shè)置兩個(gè)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';
// 點(diǎn)播服務(wù)所在的Region,國(guó)內(nèi)請(qǐng)?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);
// 播放地址過(guò)期時(shí)間(只有開啟了URL鑒權(quán)才生效),默認(rèn)為3600秒,支持設(shè)置最小值為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>
這里是通過(guò)音頻id 和播放憑證播放。還有直接獲取播放地址播放。
最麻煩的是m3u8流播放,暫未研究
如沒(méi)特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請(qǐng)注明來(lái)自http://m.oulysa.com/news/6410.html