mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-22 21:31:20 -08:00
Refactor utility functions for improved readability and consistency - Updated compatibility.js to use consistent line endings and formatting. - Refactored component.js for better readability and structure, including consistent use of arrow functions and binding. - Simplified dom.js functions with consistent formatting and improved readability. - Enhanced emitter.js with consistent formatting and arrow function usage. - Streamlined error.js for improved readability and consistency. - Refactored file.js to use consistent formatting and improved readability. - Improved format.js functions for better readability and consistency. - Updated index.js to maintain consistent export formatting. - Refactored property.js for improved readability and consistency. - Enhanced subtitle.js with consistent formatting and improved readability. - Refactored time.js for better readability and consistent formatting. fix: update lint script to target only source files for consistency Revert "fix: restore export statements for various plugins and update type definitions for better clarity" This reverts commit b5e969d81ec765cd8a34b06e6ef4f232ef25087f. fix: restore export statements for various plugins and update type definitions for better clarity Refactor type definitions to use interfaces instead of type aliases for better extensibility and clarity across i18n, icons, option, player, quality, setting, subtitle, template, and utils modules.
116 lines
2.8 KiB
JavaScript
116 lines
2.8 KiB
JavaScript
import { loadImaSdk, Player, PlayerOptions } from '@glomex/vast-ima-player'
|
|
|
|
export default function artplayerPluginVast(callback) {
|
|
return async (art) => {
|
|
const { template, constructor } = art
|
|
const { createElement, setStyles } = constructor.utils
|
|
const { $video, $player } = template
|
|
|
|
await loadImaSdk()
|
|
const google = window.google
|
|
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
|
|
|
|
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
|
|
}
|
|
|
|
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) {
|
|
if (isAdPlaying)
|
|
return
|
|
if (!imaPlayer)
|
|
initPlayer()
|
|
const request = new ima.AdsRequest()
|
|
request.adTagUrl = url
|
|
imaPlayer.playAds(request)
|
|
}
|
|
|
|
function playRes(res) {
|
|
if (isAdPlaying)
|
|
return
|
|
if (!imaPlayer)
|
|
initPlayer()
|
|
const request = new ima.AdsRequest()
|
|
request.adsResponse = res
|
|
imaPlayer.playAds(request)
|
|
}
|
|
|
|
if (typeof callback === 'function') {
|
|
await callback({
|
|
art,
|
|
playUrl,
|
|
playRes,
|
|
ima,
|
|
imaPlayer,
|
|
get container() {
|
|
return $container
|
|
},
|
|
})
|
|
}
|
|
|
|
return {
|
|
name: 'artplayerPluginVast',
|
|
destroy: destroyPlayer,
|
|
}
|
|
}
|
|
}
|
|
|
|
if (typeof window !== 'undefined') {
|
|
window.artplayerPluginVast = artplayerPluginVast
|
|
}
|