@@ -74,11 +74,10 @@ export default defineComponent({
74
74
const playerRef = ref < IVLabsPlayer | null > ( null ) ;
75
75
const uniqueId = `ivlabs-player-${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) } ` ;
76
76
77
- /**
78
- * Initializes the player when the component is mounted.
79
- */
77
+ // Initialize player only after the component is mounted and the DOM is ready
80
78
onMounted ( ( ) => {
81
- if ( containerRef . value ) {
79
+ const newContainer = containerRef . value ;
80
+ if ( newContainer && ! playerRef . value ) {
82
81
const playerConfig : PlayerConfig = {
83
82
...attrs ,
84
83
videoUrl : props . videoUrl ,
@@ -87,37 +86,30 @@ export default defineComponent({
87
86
locale : props . locale ,
88
87
} ;
89
88
90
- setTimeout ( ( ) => {
91
- try {
92
- if ( containerRef . value ) {
93
- const player = new IVLabsPlayer ( `#${ uniqueId } ` , playerConfig ) ;
94
- playerRef . value = player ;
89
+ try {
90
+ const player = new IVLabsPlayer ( `#${ uniqueId } ` , playerConfig ) ;
91
+ playerRef . value = player ;
95
92
96
- // Register event listeners if onAnalyticsEvent is provided.
97
- if ( props . onAnalyticsEvent ) {
98
- player . on ( 'PLAYER_LOADED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'PLAYER_LOADED' , payload ) ) ;
99
- player . on ( 'VIDEO_STARTED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_STARTED' , payload ) ) ;
100
- player . on ( 'VIDEO_PAUSED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_PAUSED' , payload ) ) ;
101
- player . on ( 'VIDEO_ENDED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_ENDED' , payload ) ) ;
102
- player . on ( 'CUE_TRIGGERED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'CUE_TRIGGERED' , payload ) ) ;
103
- player . on ( 'INTERACTION_COMPLETED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'INTERACTION_COMPLETED' , payload ) ) ;
104
- player . on ( 'ERROR' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'ERROR' , payload ) ) ;
105
- }
93
+ if ( props . onAnalyticsEvent ) {
94
+ player . on ( 'PLAYER_LOADED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'PLAYER_LOADED' , payload ) ) ;
95
+ player . on ( 'VIDEO_STARTED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_STARTED' , payload ) ) ;
96
+ player . on ( 'VIDEO_PAUSED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_PAUSED' , payload ) ) ;
97
+ player . on ( 'VIDEO_ENDED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'VIDEO_ENDED' , payload ) ) ;
98
+ player . on ( 'CUE_TRIGGERED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'CUE_TRIGGERED' , payload ) ) ;
99
+ player . on ( 'INTERACTION_COMPLETED' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'INTERACTION_COMPLETED' , payload ) ) ;
100
+ player . on ( 'ERROR' , ( payload ?: AnalyticsPayload ) => ( props . onAnalyticsEvent as Function ) ( 'ERROR' , payload ) ) ;
101
+ }
106
102
107
- // Load initial cues if provided.
108
- if ( props . cues ) {
109
- player . loadCues ( props . cues ) ;
110
- }
103
+ if ( props . cues ) {
104
+ player . loadCues ( props . cues ) ;
105
+ }
111
106
112
- // Load initial translations if provided.
113
- if ( props . translations ) {
114
- player . loadTranslations ( props . locale , props . translations ) ;
115
- }
116
- }
117
- } catch ( error ) {
118
- console . error ( 'Error initializing IVLabsPlayer:' , error ) ;
107
+ if ( props . translations ) {
108
+ player . loadTranslations ( props . locale , props . translations ) ;
119
109
}
120
- } , 0 ) ;
110
+ } catch ( error ) {
111
+ console . error ( 'Error initializing IVLabsPlayer:' , error ) ;
112
+ }
121
113
}
122
114
} ) ;
123
115
0 commit comments