mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 14:41:20 -08:00
更新 VAST 插件文档和类型定义,修复描述和版本号,增强广告播放器功能
This commit is contained in:
1 parent
19756e8224
commit
e773e2c44f
4 files changed
+106
-36
No files matched your search
@@ -3,7 +3,7 @@ import { Player, PlayerOptions, loadImaSdk } from '@glomex/vast-ima-player';
|
||||
export default function artplayerPluginVast(callback) {
|
||||
return async (art) => {
|
||||
const { template, constructor } = art;
|
||||
const { createElement, setStyles, append } = constructor.utils;
|
||||
const { createElement, setStyles } = constructor.utils;
|
||||
const { $video, $player } = template;
|
||||
|
||||
await loadImaSdk();
|
||||
@@ -11,45 +11,101 @@ export default function artplayerPluginVast(callback) {
|
||||
const ima = google.ima;
|
||||
|
||||
const adsRenderingSettings = new ima.AdsRenderingSettings();
|
||||
adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
|
||||
adsRenderingSettings.enablePreloading = true;
|
||||
|
||||
const playerOptions = new PlayerOptions();
|
||||
let isAdPlaying = false;
|
||||
let imaPlayer = null;
|
||||
let $container = null;
|
||||
|
||||
const id = `art-${Date.now()}`;
|
||||
const $container = createElement('div');
|
||||
append($player, $container);
|
||||
$container.id = id;
|
||||
setStyles($container, {
|
||||
position: 'absolute',
|
||||
inset: '0',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
zIndex: '150',
|
||||
display: 'none',
|
||||
});
|
||||
function createContainer() {
|
||||
const container = createElement('div');
|
||||
const id = `art-vast-${Date.now()}`;
|
||||
container.id = id;
|
||||
setStyles(container, {
|
||||
position: 'absolute',
|
||||
inset: '0',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
zIndex: '150',
|
||||
backgroundColor: 'black',
|
||||
display: 'none',
|
||||
pointerEvents: 'auto',
|
||||
});
|
||||
return container;
|
||||
}
|
||||
|
||||
const imaPlayer = new Player(ima, $video, $container, adsRenderingSettings, playerOptions);
|
||||
function initPlayer() {
|
||||
$container = createContainer();
|
||||
$player.appendChild($container);
|
||||
imaPlayer = new Player(ima, $video, $container, adsRenderingSettings, playerOptions);
|
||||
|
||||
// ✅ 事件绑定
|
||||
imaPlayer.onAdStarted = () => {
|
||||
isAdPlaying = true;
|
||||
$container.style.display = 'block';
|
||||
art.pause();
|
||||
};
|
||||
|
||||
imaPlayer.onAdEnd = () => {
|
||||
isAdPlaying = false;
|
||||
$container.style.display = 'none';
|
||||
art.play();
|
||||
};
|
||||
|
||||
imaPlayer.onAdError = (error) => {
|
||||
console.error('VAST Ad Error:', error);
|
||||
isAdPlaying = false;
|
||||
$container.style.display = 'none';
|
||||
art.play();
|
||||
};
|
||||
}
|
||||
|
||||
function destroyPlayer() {
|
||||
if ($container && $container.parentNode) {
|
||||
$container.parentNode.removeChild($container);
|
||||
}
|
||||
$container = null;
|
||||
imaPlayer = null;
|
||||
}
|
||||
|
||||
function playUrl(url) {
|
||||
const playAdsRequest = new ima.AdsRequest();
|
||||
playAdsRequest.adTagUrl = url;
|
||||
imaPlayer.playAds(playAdsRequest);
|
||||
if (isAdPlaying) return;
|
||||
if (!imaPlayer) initPlayer();
|
||||
const request = new ima.AdsRequest();
|
||||
request.adTagUrl = url;
|
||||
imaPlayer.playAds(request);
|
||||
}
|
||||
|
||||
function playRes(res) {
|
||||
const playAdsRequest = new ima.AdsRequest();
|
||||
playAdsRequest.adsResponse = res;
|
||||
imaPlayer.playAds(playAdsRequest);
|
||||
if (isAdPlaying) return;
|
||||
if (!imaPlayer) initPlayer();
|
||||
const request = new ima.AdsRequest();
|
||||
request.adsResponse = res;
|
||||
imaPlayer.playAds(request);
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
await callback({ art, id, ima, imaPlayer, $container, playUrl, playRes });
|
||||
await callback({
|
||||
art,
|
||||
playUrl,
|
||||
playRes,
|
||||
ima,
|
||||
imaPlayer,
|
||||
get container() {
|
||||
return $container;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'artplayerPluginVast',
|
||||
destroy: destroyPlayer,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window['artplayerPluginVast'] = artplayerPluginVast;
|
||||
window.artplayerPluginVast = artplayerPluginVast;
|
||||
}
|
||||
Reference in new issue
Block a user