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

View File

@@ -1,49 +1,49 @@
<template>
<div ref="videoContainer" class="video-block group relative">
<video @ended="videoEnded" class="rounded-lg">
<source :src="file" type="video/mp4" />
<video @timeupdate="updateTime" @ended="videoEnded" class="rounded-lg">
<source :src="fileURL" :type="type" />
</video>
<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">
<template #icon>
<Play
v-if="!playing"
@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>
</Button>
<span class="text-xs font-medium">
{{ formatTime(currentTime) }} / {{ formatTime(duration) }}
</span>
<input
type="range"
min="0"
:max="duration"
step="0.1"
v-model="currentTime"
@input="changeTime"
class="duration-slider"
@input="changeCurrentTime"
class="duration-slider w-full h-1"
/>
<Button variant="ghost" @click="toggleMute">
<template #icon>
<Volume2 v-if="!muted" class="w-4 h-4 fill-gray-900" />
<VolumeX v-else class="w-4 h-4 fill-gray-900" />
</template>
</Button>
<span class="text-xs font-medium">
{{ formatTime(currentTime) }} / {{ formatTime(duration) }}
</span>
<Button variant="ghost" @click="toggleFullscreen">
<template #icon>
<Maximize class="w-4 h-4 fill-gray-900" />
<Maximize class="w-4 h-4 text-gray-900" />
</template>
</Button>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ref, onMounted, computed } from 'vue'
import { Play, Pause, Maximize, Volume2, VolumeX } from 'lucide-vue-next'
import { Button } from 'frappe-ui'
@@ -59,16 +59,39 @@ const props = defineProps({
type: String,
required: true,
},
type: {
type: String,
default: 'video/mp4',
},
})
onMounted(() => {
setTimeout(() => {
videoRef.value = document.querySelector('video')
videoRef.value.onloadedmetadata = loadedMetaData
videoRef.value.ontimeupdate = updateTime
videoRef.value.onloadedmetadata = () => {
duration.value = videoRef.value.duration
}
videoRef.value.ontimeupdate = () => {
currentTime.value = videoRef.value.currentTime
}
}, 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 = () => {
videoRef.value.play()
playing.value = true
@@ -88,18 +111,10 @@ const toggleMute = () => {
muted.value = videoRef.value.muted
}
const changeTime = () => {
const changeCurrentTime = () => {
videoRef.value.currentTime = currentTime.value
}
const loadedMetaData = () => {
duration.value = videoRef.value.duration
}
const updateTime = () => {
currentTime.value = videoRef.value.currentTime
}
const formatTime = (time) => {
const minutes = Math.floor(time / 60)
const seconds = Math.floor(time % 60)
@@ -127,11 +142,37 @@ const toggleFullscreen = () => {
height: auto;
}
iframe {
width: 100%;
min-height: 500px;
}
.duration-slider {
flex: 1;
height: 0.25rem;
-webkit-appearance: none;
appearance: none;
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>

View File

@@ -15,6 +15,7 @@ export const usersStore = defineStore('lms-users', () => {
const allUsers = createResource({
url: 'lms.lms.api.get_all_users',
cache: ['allUsers'],
auto: true,
})
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 Header from '@editorjs/header'
import Paragraph from '@editorjs/paragraph'
import Embed from '@editorjs/embed'
import { CodeBox } from '@/utils/code'
import NestedList from '@editorjs/nested-list'
import InlineCode from '@editorjs/inline-code'
import { watch } from 'vue'
import { watch, createApp } from 'vue'
import dayjs from '@/utils/dayjs'
import Embed from '@editorjs/embed'
export function createToast(options) {
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() {
return [
'Pacific/Midway',