Hi! I am new here and I was trying to create a component which works like this:
- After first video(triggered by the target image) ended, user sees the preview of the next video
- User is suggested to tap the preview to play the next video
For some reason after first video played, the preview of the next video doesn’t appear. I added console.log to the addEventListener(‘ended’, () , so the function itself seems to be working.
How can I fix it?
const playVideoComponent = {
schema: {
video: {type: 'string'},
left: {type: 'string'},
leftPreview: {type: 'string'}, // URL for left video preview image
canstop: {type: 'bool'},
},
init() {
const v = document.querySelector(this.data.video)
const p = this.data.thumb && document.querySelector(this.data.thumb)
const {el} = this
const l = document.querySelector(this.data.left)
const leftPreviewEl = document.querySelector(this.data.leftPreview) // Element for left video preview image
const tapped = false
el.setAttribute('material', 'src', p || v)
el.setAttribute('class', 'cantap')
let playing = false
// Play Pause functionality (plays main video)
el.addEventListener('click', () => {
if (!playing) {
el.setAttribute('material', {
src: v,
shader: 'chromakey',
color: '0.1 0.9 0.2',
})
v.play()
playing = true
} else if (this.data.canstop) {
v.pause()
playing = false
}
})
// Transition to paused state upon main video ending
v.addEventListener('ended', () => {
playing = false // Update playing state
console.log('First video ended!')
})
// Play left video on click of preview image
leftPreviewEl.addEventListener('click', () => {
if (!playing) {
l.play()
playing = true // Update playing state
}
})
const foundTarget = () => {
if (playing) {
v.play()
}
}
const lostTarget = () => {
if (playing) {
v.pause()
}
}
el.parentNode.addEventListener('xrextrasfound', foundTarget)
el.parentNode.addEventListener('xrextraslost', lostTarget)
this.el.sceneEl.addEventListener('disconnected', () => {
el.parentNode.removeEventListener('xrextrasfound', foundTarget)
el.parentNode.removeEventListener('xrextraslost', lostTarget)
})
},
}
export {playVideoComponent}