Skip to content

Commit

Permalink
Merge pull request #5 from izkizk8/playtime
Browse files Browse the repository at this point in the history
feat: add currentTime and duration to playerStore
  • Loading branch information
wangrongding committed Apr 10, 2024
2 parents 8208a3c + 508fb0a commit 7f71007
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/components/bottom-panel/BottomPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import IconZoomin from '~/assets/icons/control-zoomin.svg?component'
import IconZoomout from '~/assets/icons/control-zoomout.svg?component'
import IconZoomToFit from '~/assets/icons/control-zoomtofit.svg?component'
import { usePlayerStore } from '~/stores/player'
import { formatSeconds } from '~/utils/index'
const playerStore = usePlayerStore()
// 将 store 中的 playStatus 转换为 ref
const { playStatus } = storeToRefs(playerStore)
const { playStatus, currentTime, duration } = storeToRefs(playerStore)
function toggleVideoPlay() {
playerStore.playStatus = !playStatus.value
Expand All @@ -43,7 +44,7 @@ function toggleVideoPlay() {
<IconPlay />
</button>
<button class="btn-control"><IconNext /></button>
<span>00:00:00 / 00:00:00</span>
<span>{{ `${formatSeconds(currentTime)} / ${formatSeconds(duration)}`}}</span>
</div>
<div class="flex gap-4">
<button class="tooltip btn-control" data-tip="放大"><IconZoomout /></button>
Expand Down
4 changes: 3 additions & 1 deletion src/components/player/CanvasPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defineProps<{
const container = ref<HTMLElement | null>(null)
let video: HTMLVideoElement
const playerStore = usePlayerStore()
const { playStatus } = storeToRefs(playerStore)
const { playStatus, currentTime, duration } = storeToRefs(playerStore)
const menuShow = ref(false)
const contextMenuPosition = ref({ x: 0, y: 0 })
const contextMenu = ref<typeof ContextMenu | null>(null)
Expand Down Expand Up @@ -140,10 +140,12 @@ function drawVideo() {
originX: 'left',
originY: 'top'
})
duration.value = video.duration
canvas.add(videoElement)
canvas.setActiveObject(videoElement)
continuouslyRepaint()
})
video.addEventListener('timeupdate', () => { currentTime.value = video.currentTime })
}
// 持续重绘
Expand Down
6 changes: 5 additions & 1 deletion src/stores/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ export const usePlayerStore = defineStore('playerStore', () => {
// 轨道数
const trackCount = computed(() => playList.value.length)

return { playStatus, togglePlay, playList, trackCount }
const currentTime = ref<number>(0)

const duration = ref<number>(0)

return { playStatus, togglePlay, playList, trackCount, currentTime, duration }
})
13 changes: 13 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function formatSeconds(seconds: number) {
seconds = Math.floor(seconds)
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const remainingSeconds = seconds % 60

const formattedTime = `${padZero(hours)}:${padZero(minutes)}:${padZero(remainingSeconds)}`
return formattedTime
}

export function padZero(number: number) {
return number.toString().padStart(2, '0')
}

0 comments on commit 7f71007

Please sign in to comment.