import { computed, defineComponent, getCurrentInstance, h, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch, withDirectives, } from 'vue' import type { PropType, Slot, SlotsType, VNode, VNodeArrayChildren } from 'vue' import { ClosePopup, QBtn, QExpansionItem, QIcon, QItem, QItemSection, QList, QMenu, QSlider, QSpinner, QTooltip, Ripple, useQuasar, } from 'quasar' import defaultIconSet from '../../icon-set/material-icons.mjs' import defaultLang from '../../lang/en-US.mjs' type MediaPlayerType = 'video' | 'audio' type CrossOrigin = 'anonymous' | 'use-credentials' | null type ClassOrStyle = string | Record | undefined type MediaSource = { /** * Path to a media source. * * @example src="https://your-server/your-video.mov" * @example src="https://your-server/your-audio.mp3" */ src?: string /** * MIME type for the media source. * * @example type="video/mp4" * @example type="audio/mp3" */ type?: string } type MediaTrack = { /** * Text track kind. * * @example kind="subtitles" * @example kind="captions" */ kind?: string /** * Label used for track selection. * * @example label="English" * @example label="Deutsch" */ label?: string /** * Path to a subtitle, caption, chapter, or metadata file. * * @example src="https://your-server/path/to/subtitles-en.vtt" */ src?: string /** * Track language identifier. * * @example srclang="en" * @example srclang="de" */ srclang?: string } type PlaybackRateOption = { /** * Displayed label for the playback speed option. * * @example .5X * @example Normal */ label: string /** * Playback speed multiplier. * * @example 0.5 * @example 1 */ value: number } type SelectOption = { label: string value: string } type RenderChild = string | number | boolean | VNode | VNodeArrayChildren | (() => unknown) type MediaPlayerMessages = { language: string mute: string noLoadAudio: string noLoadVideo: string oldBrowserAudio: string oldBrowserVideo: string pause: string play: string rate1Point5: string rate2: string rateNormal: string ratePoint5: string settings: string speed: string toggleFullscreen: string trackLanguageOff: string unmute: string waitingAudio: string waitingVideo: string } type MediaPlayerLang = { lang?: string mediaPlayer: MediaPlayerMessages } type MediaPlayerIcons = { bigPlayButton: string fullscreen: string fullscreenExit: string language: string pause: string play: string selected: string settings: string speed: string volumeDown: string volumeOff: string volumeUp: string } type MediaPlayerIconSet = { mediaPlayer: MediaPlayerIcons } type QMediaPlayerGlobal = { Component?: unknown lang?: Record iconSet?: Record } type QuasarLike = { dark: { isActive: boolean } fullscreen?: { isActive?: boolean request: (target?: Element | null) => void | Promise exit: () => void | Promise } iconSet?: { name?: string mediaPlayer?: Partial } lang?: { isoName?: string } } type MediaPlayerState = { errorText: string | null controls: boolean showControls: boolean inControls: boolean volume: number muted: boolean currentTime: number duration: number durationTime: string remainingTime: string displayTime: string inFullscreen: boolean loading: boolean playReady: boolean playing: boolean playbackRates: PlaybackRateOption[] playbackRate: number trackLanguage: string showBigPlayButton: boolean metadataLoaded: boolean spinnerSize: string bottomControls: boolean noControls: boolean } interface QMediaPlayerSlots { /** * Replace the default old browser fallback text. * * @applicable Audio | Video */ oldbrowser: () => VNode[] /** * Render custom overlay content over the video frame. * * @applicable Video */ overlay: () => VNode[] /** * Replace the default error window. * * @applicable Audio | Video */ errorWindow: () => VNode[] /** * Replace the default controls. * * @applicable Audio | Video */ controls: () => VNode[] /** * Replace the default spinner/loading icon. * * @applicable Audio | Video */ spinner: () => VNode[] /** * Replace the default big play button. * * @applicable Video */ bigPlayButton: () => VNode[] /** * Replace the default display time. * * @applicable Audio | Video */ displayTime: () => VNode[] /** * Replace the default playback position slider. * * @applicable Audio | Video */ positionSlider: () => VNode[] /** * Replace the default duration time. * * @applicable Audio | Video */ durationTime: () => VNode[] /** * Replace the default play/pause control. * * @applicable Audio | Video */ play: () => VNode[] /** * Replace the default volume button. * * @applicable Audio | Video */ volume: () => VNode[] /** * Replace the default volume slider. * * @applicable Audio | Video */ volumeSlider: () => VNode[] /** * Replace the default settings button. * * @applicable Video */ settings: () => VNode[] /** * Replace the default settings menu content. * * @applicable Video */ settingsMenu: () => VNode[] /** * Replace the default fullscreen button. * * @applicable Video */ fullscreen: () => VNode[] } declare global { interface Window { QMediaPlayer?: QMediaPlayerGlobal } } const defaultMediaPlayerLang = defaultLang as MediaPlayerLang const defaultMediaPlayerIconSet = defaultIconSet as MediaPlayerIconSet const matClose = 'M0 0h24v24H0z@@fill:none;&&M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z' const iconSetLoaders = { 'bootstrap-icons': () => import('../../icon-set/bootstrap-icons.mjs'), 'eva-icons': () => import('../../icon-set/eva-icons.mjs'), 'fontawesome-v7': () => import('../../icon-set/fontawesome-v7.mjs'), 'ionicons-v4': () => import('../../icon-set/ionicons-v4.mjs'), 'ionicons-v8': () => import('../../icon-set/ionicons-v8.mjs'), 'line-awesome': () => import('../../icon-set/line-awesome.mjs'), 'material-icons': async () => ({ default: defaultIconSet }), 'material-icons-outlined': () => import('../../icon-set/material-icons-outlined.mjs'), 'material-icons-round': () => import('../../icon-set/material-icons-round.mjs'), 'material-icons-sharp': () => import('../../icon-set/material-icons-sharp.mjs'), 'material-symbols-outlined': () => import('../../icon-set/material-symbols-outlined.mjs'), 'material-symbols-rounded': () => import('../../icon-set/material-symbols-rounded.mjs'), 'material-symbols-sharp': () => import('../../icon-set/material-symbols-sharp.mjs'), 'mdi-v7': () => import('../../icon-set/mdi-v7.mjs'), 'svg-bootstrap-icons': () => import('../../icon-set/svg-bootstrap-icons.mjs'), 'svg-eva-icons': () => import('../../icon-set/svg-eva-icons.mjs'), 'svg-fontawesome-v7': () => import('../../icon-set/svg-fontawesome-v7.mjs'), 'svg-ionicons-v4': () => import('../../icon-set/svg-ionicons-v4.mjs'), 'svg-ionicons-v8': () => import('../../icon-set/svg-ionicons-v8.mjs'), 'svg-line-awesome': () => import('../../icon-set/svg-line-awesome.mjs'), 'svg-material-icons': () => import('../../icon-set/svg-material-icons.mjs'), 'svg-material-icons-outlined': () => import('../../icon-set/svg-material-icons-outlined.mjs'), 'svg-material-icons-round': () => import('../../icon-set/svg-material-icons-round.mjs'), 'svg-material-icons-sharp': () => import('../../icon-set/svg-material-icons-sharp.mjs'), 'svg-material-symbols-outlined': () => import('../../icon-set/svg-material-symbols-outlined.mjs'), 'svg-material-symbols-rounded': () => import('../../icon-set/svg-material-symbols-rounded.mjs'), 'svg-material-symbols-sharp': () => import('../../icon-set/svg-material-symbols-sharp.mjs'), 'svg-mdi-v7': () => import('../../icon-set/svg-mdi-v7.mjs'), 'svg-themify': () => import('../../icon-set/svg-themify.mjs'), themify: () => import('../../icon-set/themify.mjs'), } const langLoaders = { ar: () => import('../../lang/ar.mjs'), 'az-Latn': () => import('../../lang/az-Latn.mjs'), bg: () => import('../../lang/bg.mjs'), bn: () => import('../../lang/bn.mjs'), ca: () => import('../../lang/ca.mjs'), cs: () => import('../../lang/cs.mjs'), da: () => import('../../lang/da.mjs'), de: () => import('../../lang/de.mjs'), el: () => import('../../lang/el.mjs'), 'en-GB': () => import('../../lang/en-GB.mjs'), 'en-US': async () => ({ default: defaultLang }), eo: () => import('../../lang/eo.mjs'), es: () => import('../../lang/es.mjs'), et: () => import('../../lang/et.mjs'), fa: () => import('../../lang/fa.mjs'), 'fa-IR': () => import('../../lang/fa-IR.mjs'), fi: () => import('../../lang/fi.mjs'), fr: () => import('../../lang/fr.mjs'), gn: () => import('../../lang/gn.mjs'), he: () => import('../../lang/he.mjs'), hr: () => import('../../lang/hr.mjs'), hu: () => import('../../lang/hu.mjs'), id: () => import('../../lang/id.mjs'), is: () => import('../../lang/is.mjs'), it: () => import('../../lang/it.mjs'), ja: () => import('../../lang/ja.mjs'), km: () => import('../../lang/km.mjs'), 'ko-KR': () => import('../../lang/ko-KR.mjs'), 'kur-CKB': () => import('../../lang/kur-CKB.mjs'), lt: () => import('../../lang/lt.mjs'), lu: () => import('../../lang/lu.mjs'), lv: () => import('../../lang/lv.mjs'), ml: () => import('../../lang/ml.mjs'), ms: () => import('../../lang/ms.mjs'), 'nb-NO': () => import('../../lang/nb-NO.mjs'), nl: () => import('../../lang/nl.mjs'), pl: () => import('../../lang/pl.mjs'), pt: () => import('../../lang/pt.mjs'), 'pt-BR': () => import('../../lang/pt-BR.mjs'), ro: () => import('../../lang/ro.mjs'), ru: () => import('../../lang/ru.mjs'), sk: () => import('../../lang/sk.mjs'), sl: () => import('../../lang/sl.mjs'), sr: () => import('../../lang/sr.mjs'), 'sr-CYR': () => import('../../lang/sr-CYR.mjs'), sv: () => import('../../lang/sv.mjs'), ta: () => import('../../lang/ta.mjs'), th: () => import('../../lang/th.mjs'), tr: () => import('../../lang/tr.mjs'), ug: () => import('../../lang/ug.mjs'), uk: () => import('../../lang/uk.mjs'), vi: () => import('../../lang/vi.mjs'), 'zh-CN': () => import('../../lang/zh-CN.mjs'), 'zh-TW': () => import('../../lang/zh-TW.mjs'), } function getLanguageCandidates(lang: string) { const candidates = [lang] const [baseLang] = lang.split('-') if (baseLang && baseLang !== lang) { candidates.push(baseLang) } if (!candidates.includes('en-US')) { candidates.push('en-US') } return candidates } function getLanguageGlobalName(lang: string) { return lang.replace(/-([A-Za-z])/g, (_, char: string) => char.toUpperCase()) } function resolveLangLoaderName(lang: string): keyof typeof langLoaders { const candidates = getLanguageCandidates(lang) for (const candidate of candidates) { if (Object.prototype.hasOwnProperty.call(langLoaders, candidate)) { return candidate as keyof typeof langLoaders } } return 'en-US' } function hSlot(slot: Slot | undefined, otherwise: RenderChild): RenderChild { return slot !== void 0 ? slot() : otherwise } const padTime = (val: number) => { val = Math.floor(val) if (val < 10) { return '0' + val } return val + '' } const timeParse = (sec: number) => { let min = 0 min = Math.floor(sec / 60) sec = sec - min * 60 return padTime(min) + ':' + padTime(sec) } export default defineComponent({ name: 'QMediaPlayer', directives: { ClosePopup, Ripple, }, slots: Object as SlotsType, props: { /** * Tells the component which player is to be used. * * @category behavior * @applicable Audio | Video * @values video | audio * @example type="video" * @example type="audio" */ type: { type: String as PropType, required: false, default: 'video', validator: (v: string) => ['video', 'audio'].includes(v), }, /** * Use mobile logic for handling visibility of controls. This mode is best for mobiles, but can be used on desktop as well. It prevents the controls from displaying/hiding with mouse hover. Instead, clicks are used to show/hide the controls. * * @category behavior * @applicable Video */ mobileMode: Boolean, /** * When true, clicking or tapping the video frame toggles play/pause. Turn this off when the overlay slot contains interactive content, such as forms or buttons. * * @category behavior * @applicable Video * @example :toggle-play-on-click="false" */ togglePlayOnClick: { type: Boolean, default: true, }, /** * Direct media source URL. When this is set, the media element `src` is set directly and the `sources` prop is ignored. Clearing this prop unloads the media when `sources` is also empty. * * @category model * @applicable Audio | Video * @example source="https://path/to/the/video.mpeg" */ source: String, /** * One or more sources for video or audio. The browser picks the best source based on supported codecs. An empty array unloads the media when `source` is also empty. * * @category model * @applicable Audio | Video * @tsType MediaSource[] * @example :sources="[{ src: 'https://your-server/your-video.mov', type: 'video/mp4' }]" * @example :sources="[{ src: 'https://your-server/your-audio.mp3', type: 'audio/mp3' }]" */ sources: { type: Array as PropType, default: () => [], }, /** * Poster image to display before the video is loaded. * * @category model * @applicable Video * @example poster="https://path/to/the/image.jpg" */ poster: { type: String, default: '', }, /** * Fallback poster image to display when `poster` is not provided. The `poster` prop always takes precedence. * * @category model * @applicable Video * @example fallback-poster="https://path/to/the/fallback-image.jpg" */ fallbackPoster: { type: String, default: '', }, /** * One or more text tracks for subtitles, captions, chapters, or metadata. * * @category model * @applicable Video * @tsType MediaTrack[] * @example :tracks="[{ src: 'https://your-server/path/to/subtitles-en.vtt', kind: 'subtitles', srclang: 'en', label: 'English' }]" */ tracks: { type: Array as PropType, default: () => [], }, /** * Display controls in a compact single-line layout. * * @category style * @applicable Audio | Video */ dense: Boolean, /** * Automatically start playback when the media is ready to play. * * @category behavior * @applicable Audio | Video */ autoplay: Boolean, /** * Pause playback when the player scrolls completely out of the viewport. QMediaPlayer will not automatically resume playback when it becomes visible again. * * @category behavior * @applicable Audio | Video * @example auto-pause */ autoPause: { type: Boolean, default: false, }, /** * Whether to use CORS for fetching media assets. * * @category behavior * @applicable Audio | Video * @values null | anonymous | use-credentials * @example cross-origin="anonymous" */ crossOrigin: { type: String as PropType, default: null, validator: (v: string | null) => v === null || ['anonymous', 'use-credentials'].includes(v), }, /** * Initial volume from 0-100, as a percentage. * * @category behavior * @applicable Audio | Video * @example :volume="50" * @example :volume="75" */ volume: { type: Number, default: 60, validator: (v: number) => v >= 0 && v <= 100, }, /** * Hide the volume slider control. * * @category behavior * @applicable Audio | Video */ hideVolumeSlider: Boolean, /** * Hide the volume button and volume slider. * * @category behavior * @applicable Audio | Video * @since v1.1.0 */ hideVolumeBtn: Boolean, /** * Hide the play/pause button. * * @category behavior * @applicable Audio | Video * @since v1.0.21 */ hidePlayBtn: Boolean, /** * Hide the settings button. * * @category behavior * @applicable Audio | Video * @since v1.0.2 */ hideSettingsBtn: Boolean, /** * Hide the fullscreen button. * * @category behavior * @applicable Video * @since v1.2.0 */ hideFullscreenBtn: Boolean, /** * Disable the seek-to-position slider. * * @category behavior * @applicable Audio | Video * @since v1.1.0 */ disabledSeek: Boolean, /** * Provides a hint to the browser about what the author thinks will lead to the best user experience. * * @category state * @applicable Video * @values none | metadata | auto * @tsType 'none' | 'metadata' | 'auto' * @example preload="metadata" */ preload: { type: String, default: 'metadata', validator: (v: string) => ['none', 'metadata', 'auto'].includes(v), }, /** * Force the player to use an `