1. 首先在阿里云控制中心找到视频点播,如果没有开通就需要先开通服务,最好先购买存储流量包和播放流量包,不然就会以播放流量收费比较贵。
- 添加加速域名,默认加速是上海地区,在配置管理-存储管理可以找到存储地址
- 添加好域名就要配置安全策略,比例黑白名单,指定只能在哪个域名下播放,是否支持浏览器直接打开等一些防止视频被别人下载,需要在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,国内请填cn-shanghai,不要填写别的区域
$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}',
//必选参数。音视频ID。示例:1e067a2831b641db90d570b6480f****。
playauth : '{$PlayAuth}',
//必选参数。音视频播放凭证。
},function(player){
console.log('The player is created.')
});
</script>
这里是通过音频id 和播放凭证播放。还有直接获取播放地址播放。
最麻烦的是m3u8流播放,暂未研究