feat: video player

This commit is contained in:
Jannat Patel
2024-06-20 16:46:50 +05:30
parent 3ba8805413
commit 05e8513ad1
5 changed files with 159 additions and 51 deletions

View File

@@ -1,19 +1,20 @@
<template> <template>
<div> <div>
<audio width="100%" controls controlsList="nodownload" class="mb-4"> <!-- <audio width="100%" controls controlsList="nodownload" class="mb-4">
<source :src="encodeURI(file)" type="audio/mp3" />
</audio> -->
<audio @ended="handleAudioEnd" controlsList="nodownload" class="mb-4">
<source :src="encodeURI(file)" type="audio/mp3" /> <source :src="encodeURI(file)" type="audio/mp3" />
</audio> </audio>
<audio width="100%" controlsList="nodownload" class="mb-4"> <div
<source :src="encodeURI(file)" type="audio/mp3" /> class="flex items-center space-x-2 shadow rounded-lg p-1 w-1/2 mx-auto"
</audio> >
<div class="flex items-center space-x-4 p-2 shadow rounded-2xl">
<Button variant="ghost" @click="togglePlay"> <Button variant="ghost" @click="togglePlay">
<template #icon> <template #icon>
<Play v-if="!isPlaying" class="w-4 h-4 fill-gray-900" /> <Play v-if="!isPlaying" class="w-4 h-4 text-gray-900" />
<Pause v-else class="w-4 h-4 fill-gray-900" /> <Pause v-else class="w-4 h-4 text-gray-900" />
</template> </template>
</Button> </Button>
<input <input
type="range" type="range"
min="0" min="0"
@@ -21,15 +22,15 @@
step="0.1" step="0.1"
v-model="currentTime" v-model="currentTime"
@input="changeCurrentTime" @input="changeCurrentTime"
class="duration-slider" class="duration-slider w-full h-1"
/> />
<span class="text-xs text-gray-900 font-medium" <span class="text-xs text-gray-900 font-medium">
>{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</span {{ formatTime(currentTime) }} / {{ formatTime(duration) }}
> </span>
<Button variant="ghost" @click="toggleMute"> <Button variant="ghost" @click="toggleMute">
<template #icon> <template #icon>
<Volume2 v-if="!isMuted" class="w-4 h-4 fill-gray-900" /> <Volume2 v-if="!isMuted" class="w-4 h-4 text-gray-900" />
<VolumeX v-else class="w-4 h-4 fill-gray-900" /> <VolumeX v-else class="w-4 h-4 text-gray-900" />
</template> </template>
</Button> </Button>
</div> </div>
@@ -44,7 +45,6 @@ import { Button } from 'frappe-ui'
const isPlaying = ref(false) const isPlaying = ref(false)
const audio = ref(null) const audio = ref(null)
let isMuted = ref(false) let isMuted = ref(false)
let volume = ref(1)
let currentTime = ref(0) let currentTime = ref(0)
let duration = ref(0) let duration = ref(0)
@@ -60,7 +60,6 @@ onMounted(() => {
audio.value = document.querySelector('audio') audio.value = document.querySelector('audio')
console.log(audio.value) console.log(audio.value)
audio.value.onloadedmetadata = () => { audio.value.onloadedmetadata = () => {
console.log(audio.value.duration)
duration.value = audio.value.duration duration.value = audio.value.duration
} }
audio.value.ontimeupdate = () => { audio.value.ontimeupdate = () => {
@@ -88,6 +87,10 @@ const changeCurrentTime = () => {
audio.value.currentTime = currentTime.value audio.value.currentTime = currentTime.value
} }
const handleAudioEnd = () => {
isPlaying.value = false
}
const formatTime = (time) => { const formatTime = (time) => {
const minutes = Math.floor(time / 60) const minutes = Math.floor(time / 60)
const seconds = Math.floor(time % 60) const seconds = Math.floor(time % 60)
@@ -105,13 +108,30 @@ watch(isPlaying, (newVal) => {
<style> <style>
.duration-slider { .duration-slider {
flex: 1; flex: 1;
height: 0.25rem;
-webkit-appearance: none; -webkit-appearance: none;
appearance: none; appearance: none;
background-color: theme('colors.gray.400'); background-color: theme('colors.gray.400');
cursor: pointer;
} }
.duration-slider::-moz-range-track { .duration-slider::-webkit-slider-thumb {
background: theme('colors.gray.900'); height: 10px;
width: 10px;
-webkit-appearance: none;
background-color: theme('colors.gray.900');
}
@media screen and (-webkit-min-device-pixel-ratio: 0) {
input[type='range'] {
overflow: hidden;
width: 150px;
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
cursor: pointer;
box-shadow: -150px 0 0 150px theme('colors.gray.900');
}
} }
</style> </style>

View File

@@ -1,49 +1,49 @@
<template> <template>
<div ref="videoContainer" class="video-block group relative"> <div ref="videoContainer" class="video-block group relative">
<video @ended="videoEnded" class="rounded-lg"> <video @timeupdate="updateTime" @ended="videoEnded" class="rounded-lg">
<source :src="file" type="video/mp4" /> <source :src="fileURL" :type="type" />
</video> </video>
<div <div
class="flex items-center space-x-4 bg-gray-200 rounded-2xl p-0.5 absolute bottom-3 w-[98%] invisible group-hover:visible left-0 right-0 mx-auto" class="flex items-center space-x-2 bg-gray-200 rounded-lg p-0.5 absolute bottom-3 w-[98%] left-0 right-0 mx-auto"
> >
<Button variant="ghost"> <Button variant="ghost">
<template #icon> <template #icon>
<Play <Play
v-if="!playing" v-if="!playing"
@click="playVideo" @click="playVideo"
class="w-4 h-4 fill-gray-900" class="w-4 h-4 text-gray-900"
/> />
<Pause v-else @click="pauseVideo" class="w-4 h-4 fill-gray-900" /> <Pause v-else @click="pauseVideo" class="w-4 h-4 text-gray-900" />
</template>
</Button>
<Button variant="ghost" @click="toggleMute">
<template #icon>
<Volume2 v-if="!muted" class="w-4 h-4 text-gray-900" />
<VolumeX v-else class="w-4 h-4 text-gray-900" />
</template> </template>
</Button> </Button>
<span class="text-xs font-medium">
{{ formatTime(currentTime) }} / {{ formatTime(duration) }}
</span>
<input <input
type="range" type="range"
min="0" min="0"
:max="duration" :max="duration"
step="0.1" step="0.1"
v-model="currentTime" v-model="currentTime"
@input="changeTime" @input="changeCurrentTime"
class="duration-slider" class="duration-slider w-full h-1"
/> />
<Button variant="ghost" @click="toggleMute"> <span class="text-xs font-medium">
<template #icon> {{ formatTime(currentTime) }} / {{ formatTime(duration) }}
<Volume2 v-if="!muted" class="w-4 h-4 fill-gray-900" /> </span>
<VolumeX v-else class="w-4 h-4 fill-gray-900" />
</template>
</Button>
<Button variant="ghost" @click="toggleFullscreen"> <Button variant="ghost" @click="toggleFullscreen">
<template #icon> <template #icon>
<Maximize class="w-4 h-4 fill-gray-900" /> <Maximize class="w-4 h-4 text-gray-900" />
</template> </template>
</Button> </Button>
</div> </div>
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref, onMounted } from 'vue' import { ref, onMounted, computed } from 'vue'
import { Play, Pause, Maximize, Volume2, VolumeX } from 'lucide-vue-next' import { Play, Pause, Maximize, Volume2, VolumeX } from 'lucide-vue-next'
import { Button } from 'frappe-ui' import { Button } from 'frappe-ui'
@@ -59,16 +59,39 @@ const props = defineProps({
type: String, type: String,
required: true, required: true,
}, },
type: {
type: String,
default: 'video/mp4',
},
}) })
onMounted(() => { onMounted(() => {
setTimeout(() => { setTimeout(() => {
videoRef.value = document.querySelector('video') videoRef.value = document.querySelector('video')
videoRef.value.onloadedmetadata = loadedMetaData videoRef.value.onloadedmetadata = () => {
videoRef.value.ontimeupdate = updateTime duration.value = videoRef.value.duration
}
videoRef.value.ontimeupdate = () => {
currentTime.value = videoRef.value.currentTime
}
}, 0) }, 0)
}) })
const fileURL = computed(() => {
if (isYoutube) {
let url = props.file
if (url.includes('watch?v=')) {
url = url.replace('watch?v=', 'embed/')
}
return `${url}?autoplay=0&controls=0&disablekb=1&playsinline=1&cc_load_policy=1&cc_lang_pref=auto`
}
return props.file
})
const isYoutube = computed(() => {
return props.type == 'video/youtube'
})
const playVideo = () => { const playVideo = () => {
videoRef.value.play() videoRef.value.play()
playing.value = true playing.value = true
@@ -88,18 +111,10 @@ const toggleMute = () => {
muted.value = videoRef.value.muted muted.value = videoRef.value.muted
} }
const changeTime = () => { const changeCurrentTime = () => {
videoRef.value.currentTime = currentTime.value videoRef.value.currentTime = currentTime.value
} }
const loadedMetaData = () => {
duration.value = videoRef.value.duration
}
const updateTime = () => {
currentTime.value = videoRef.value.currentTime
}
const formatTime = (time) => { const formatTime = (time) => {
const minutes = Math.floor(time / 60) const minutes = Math.floor(time / 60)
const seconds = Math.floor(time % 60) const seconds = Math.floor(time % 60)
@@ -127,11 +142,37 @@ const toggleFullscreen = () => {
height: auto; height: auto;
} }
iframe {
width: 100%;
min-height: 500px;
}
.duration-slider { .duration-slider {
flex: 1; flex: 1;
height: 0.25rem;
-webkit-appearance: none; -webkit-appearance: none;
appearance: none; appearance: none;
background-color: theme('colors.gray.400'); background-color: theme('colors.gray.400');
cursor: pointer;
}
.duration-slider::-webkit-slider-thumb {
height: 10px;
width: 10px;
-webkit-appearance: none;
background-color: theme('colors.gray.900');
}
@media screen and (-webkit-min-device-pixel-ratio: 0) {
input[type='range'] {
overflow: hidden;
width: 150px;
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
cursor: pointer;
box-shadow: -500px 0 0 500px theme('colors.gray.900');
}
} }
</style> </style>

View File

@@ -15,6 +15,7 @@ export const usersStore = defineStore('lms-users', () => {
const allUsers = createResource({ const allUsers = createResource({
url: 'lms.lms.api.get_all_users', url: 'lms.lms.api.get_all_users',
cache: ['allUsers'], cache: ['allUsers'],
auto: true,
}) })
return { return {

View File

@@ -0,0 +1,32 @@
import Embed from '@editorjs/embed'
import VideoBlock from '@/components/VideoBlock.vue'
import { createApp } from 'vue'
export class CustomEmbed extends Embed {
render() {
const container = super.render()
const { service, source, embed } = this.data
if (service === 'youtube' || service === 'vimeo') {
// Remove the iframe or existing embed content
container.innerHTML = ''
// Create a placeholder element for Vue component
const vueContainer = document.createElement('div')
vueContainer.setAttribute('data-service', service)
vueContainer.setAttribute('data-video-id', this.data.source)
// Append the Vue placeholder
container.appendChild(vueContainer)
console.log(source)
// Mount the Vue component (using a global Vue instance)
const app = createApp(VideoBlock, {
file: source,
type: 'video/youtube',
})
app.mount(vueContainer)
}
return container
}
}

View File

@@ -4,12 +4,12 @@ import { Quiz } from '@/utils/quiz'
import { Upload } from '@/utils/upload' import { Upload } from '@/utils/upload'
import Header from '@editorjs/header' import Header from '@editorjs/header'
import Paragraph from '@editorjs/paragraph' import Paragraph from '@editorjs/paragraph'
import Embed from '@editorjs/embed'
import { CodeBox } from '@/utils/code' import { CodeBox } from '@/utils/code'
import NestedList from '@editorjs/nested-list' import NestedList from '@editorjs/nested-list'
import InlineCode from '@editorjs/inline-code' import InlineCode from '@editorjs/inline-code'
import { watch } from 'vue' import { watch, createApp } from 'vue'
import dayjs from '@/utils/dayjs' import dayjs from '@/utils/dayjs'
import Embed from '@editorjs/embed'
export function createToast(options) { export function createToast(options) {
toast({ toast({
@@ -179,6 +179,20 @@ export function getEditorTools() {
} }
} }
function getVideoBlockHtml(medium) {
let regex
if (medium === 'youtube') {
regex =
/(?:https?:\/\/)?(?:www\.)?(?:(?:youtu\.be\/)|(?:youtube\.com)\/(?:v\/|u\/\w\/|embed\/|watch))(?:(?:\?v=)?([^#&?=]*))?((?:[?&]\w*=\w*)*)/
regex.exec(url)?.slice(1)
}
const wrapper = document.createElement('div')
const app = createApp(VideoBlock, {
file: file.file_url,
})
app.mount(wrapper)
}
export function getTimezones() { export function getTimezones() {
return [ return [
'Pacific/Midway', 'Pacific/Midway',