配置文件:
/*阿里云视频相关配置 */
vodAccessKeyId:
'LTAI0OVcwKRNQupi',
vodSecretAccessKey:
'EQtWDv89LqUXwRjBd044UpZmIoEgCq',
vodEndpoint:
'http://vod.cn-shanghai.aliyuncs.com',
vodApiVersion:
'2017-03-21'
控制层:
async
getvideoplayauthAction() {
var
VideoId
=
this.
param(
"vid");
if (
VideoId
&&
VideoId.length
>
0) {
var
client
=
new
RPCClient({
accessKeyId:
this.
config(
"vodAccessKeyId"),
secretAccessKey:
this.
config(
"vodSecretAccessKey"),
endpoint:
this.
config(
"vodEndpoint"),
apiVersion:
this.
config(
"vodApiVersion")
});
var
res
=
await
client.
request(
'GetVideoPlayAuth', { VideoId:
VideoId, AuthInfoTimeout:
100 });
// this.logger.info('Aliyun-VOD GetVideoPlayAuth:%s', JSON.stringify(res));
console.
log(
'Aliyun-VOD GetVideoPlayAuth:%s', JSON.
stringify(
res));
this.
json({
'errno':
0,
'data':
res});
}
else {
this.
json({
'errno':
1,
'errmsg':
"视频id不存在"});
}
}
页面
<
link
rel=
"stylesheet"
href=
"//g.alicdn.com/de/prismplayer/2.7.1/skins/default/aliplayer-min.css"
/>
<
script
type=
"text/javascript"
src=
"//g.alicdn.com/de/prismplayer/2.7.1/aliplayer-min.js"></
script>
容器
<
div
class=
"prism-player"
id=
"J_prismPlayer"
style=
"display: none;width:100%; height:200px;"></
div>
实例化
var
player
;
var
vid = "c4d4915538dc4528a7f6c676d06ed08b"
// 视频id
var
isLimited
=
true
;
// 当前用户观看时长是否受限
var
limitTimeLen
=
180
;
// 受限时长,单位秒
var
int
;
$
(
function
(){
$
.
post
(
"/openapi/video/getvideoplayauth"
,{vid:
vid
},
function
(res){
if
(
res
.
errno
==
0
){
var
obj
=
res
.data;
player
=
new
Aliplayer
({
id:
'J_prismPlayer'
,
width:
'100%'
,
autoplay:
false
,
//支持播放地址播放,此播放优先级最高
// source : '播放url',
//播放方式二:点播用户推荐
vid:
vid
,
playauth:
obj
.
PlayAuth
,
cover:
obj
.
VideoMeta
.
CoverURL
,
//'http://liveroom-img.oss-cn-qingdao.aliyuncs.com/logo.png',
//播放方式三:仅MPS用户使用
// vid : '1e067a2831b641db90d570b6480fbc40',
// accId: '',
// accSecret: '',
// stsToken: '',
// domainRegion: '',
// authInfo: '',
//播放方式四:使用STS方式播放
// vid : '1e067a2831b641db90d570b6480fbc40',
// accessKeyId: '',
// securityToken: '',
// accessKeySecret: ''
},
function
(player) {
//console.log('播放器创建好了。')
});
player
.
on
(
"ended"
,
endedHandle
);
// player.on("completeSeek", test);
if
(
isLimited
==
true
){
initLimitCheck
();
}
}
else
{
//alert(res.msg);
}
})
});
function
endedHandle
() {
var
newPlayAuth
=
""
;
$
.
post
(
"/openapi/video/getvideoplayauth"
,{vid:
vid
},
function
(res){
console
.
log
(
res
)
if
(
res
.
errno
==
0
){
var
obj
=
res
.data;
player
.
replayByVidAndPlayAuth
(
vid
,
obj
.
PlayAuth
);
}
else
{
alert
(
res
.
msg
);
}
})
}
function
initLimitCheck
() {
if
(
player
) {
player
.
on
(
"completeSeek"
,
checkLimitTime
);
player
.
on
(
"play"
,
checkLimitTime
);
int
=
window.
setInterval
(
"checkLimitTime()"
,
2000
);
}
}
function
checkLimitTime
(e) {
//console.log(e)
if
(
player
) {
var
curTime
=
0
;
if
(
e
&&
e
.type
==
"completeSeek"
) {
curTime
=
parseFloat
(
e
.
paramData
);
//console.log("seek", curTime);
}
else
{
if
(
player
.
getStatus
()
==
'playing'
||
player
.
getStatus
()
==
'loading'
||
player
.
getStatus
()
==
'play'
||
player
.
getStatus
()
==
'pause'
) {
curTime
=
player
.
getCurrentTime
();
// console.log("play", curTime);
}
}
if
(
curTime
>=
limitTimeLen
) {
player
.
pause
();
alert
(
"您还未购买该课程,请购买后观看剩余内容!"
);
window.
clearInterval
(
int
);
// player = null;
}
}
}