9433cfb9创建于 2025年12月31日历史提交
<template>
	<view class="container">
		<!-- 视频组件 -->
		<video id="myVideo" :src="src" :controls="true" autoplay="false" class="video" @play="onVideoPlay">
		</video>
		<!-- 信息流广告 - 覆盖在视频上方 -->
		<view v-if="adVisible" class="ad-container">
			<ad adpid="1597617406" style="width: 100%;height: 420rpx;" @load="loadFun" @error="errorFun" @close="closeAd"></ad>
		</view>
	</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: {
			// 视频播放时触发广告
			onVideoPlay() {
				if (!this.showAd) {
					this.showAd = true;
					this.adVisible = true;
					const videoContext = uni.createVideoContext('myVideo')
					videoContext!.stop();
				}
			},
			// 关闭广告
			closeAd() {
				console.log("执行了关闭");
				// this.showAd = false
				this.adVisible = false;
				// 确保视频恢复播放
				const videoContext = uni.createVideoContext('myVideo')
				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;
	}

	.ad-container {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 420rpx;
		background-color: rgba(0, 0, 0, 0.7);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 999;
	}
</style>