<template>
<view class="container">
<!-- 视频组件 -->
<video id="myVideo2" :src="src" :controls="true" autoplay="false" class="video" @play="onVideoPlay">
<!-- 信息流广告 - 覆盖在视频上方 -->
<view v-if="adVisible" class="ad-container">
<ad adpid="1597617406" class="ad-class" @load="loadFun" @error="errorFun" @close="closeAd"></ad>
</view>
</video>
</view>
</template>
<script>
export default {
data() {
return {
title: '广告演示',
src: "https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4",
showAd: false,
adVisible: false,
countdown: 10,
adTimer: null
}
},
onLoad() {
// 页面加载逻辑
},
methods: {
setfs(){
const videoContext = uni.createVideoContext('myVideo2');
const direction : RequestFullScreenOptions = {direction:-90} as RequestFullScreenOptions;
videoContext!.requestFullScreen(direction);
},
// 视频播放时触发广告
onVideoPlay() {
if (!this.showAd) {
const videoContext = uni.createVideoContext('myVideo2')
videoContext!.stop();
this.showAd = true;
const direction : RequestFullScreenOptions = {direction:90} as RequestFullScreenOptions;
videoContext!.requestFullScreen(direction);
this.adVisible = true;
}
},
// 关闭广告
closeAd() {
console.log("执行了关闭");
// this.showAd = false
this.adVisible = false;
// 确保视频恢复播放
const videoContext = uni.createVideoContext('myVideo2')
videoContext!.play()
},
loadFun(){
console.log("加载成功");
},
errorFun(e : UniAdErrorEvent){
console.log(e.detail);
}
}
}
</script>
<style>
.container {
position: relative;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
/* align-items: center; */
}
.video {
width: 100%;
height: 420rpx;
display: flex;
align-items: center;
justify-content: center;
}
.ad-class{
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.ad-container {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
}
</style>