- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.7k
 
feat: add pause-by-duration control for SlidevVideo #2236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Jeffrey0117
  wants to merge
  5
  commits into
  slidevjs:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Jeffrey0117:feat-slidevvideo-pause
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      20e45cf
              
                feat: support pause=[1,2,3,'end'] in SlidevVideo
              
              
                booxing1999 849ff7a
              
                del br
              
              
                Jeffrey0117 ec1e80e
              
                del garbege
              
              
                Jeffrey0117 e61afc0
              
                docs: add SlidevVideo controlled playback demo
              
              
                booxing1999 e26ddc6
              
                Merge remote-tracking branch 'upstream/feat-slidevvideo-pause' into f…
              
              
                booxing1999 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,103 +1,40 @@ | ||
| <br> | ||
| <p align="center"> | ||
| <a href="https://sli.dev" target="_blank"> | ||
| <img src="https://sli.dev/logo-title.png" alt="Slidev" height="250" width="250"/> | ||
| </a> | ||
| </p> | ||
| 
     | 
||
| <p align="center"> | ||
| Presentation <b>slide</b>s for <b>dev</b>elopers 🧑💻👩💻👨💻 | ||
| </p> | ||
| 
     | 
||
| <p align="center"> | ||
| <a href="https://www.npmjs.com/package/@slidev/cli" target="__blank"><img src="https://img.shields.io/npm/v/@slidev/cli?color=2B90B6&label=" alt="NPM version"></a> | ||
| <a href="https://www.npmjs.com/package/@slidev/cli" target="__blank"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/@slidev/cli?color=349dbe&label="></a> | ||
| <a href="https://sli.dev/" target="__blank"><img src="https://img.shields.io/static/v1?label=&message=docs%20%26%20demos&color=45b8cd" alt="Docs & Demos"></a> | ||
| <a href="https://sli.dev/resources/theme-gallery" target="__blank"><img src="https://img.shields.io/static/v1?label=&message=themes&color=4ec5d4" alt="Themes"></a> | ||
| <br> | ||
| <a href="https://github.com/slidevjs/slidev" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/slidevjs/slidev?style=social"></a> | ||
| </p> | ||
| 
     | 
||
| <p align="center"> | ||
| <a href="https://twitter.com/antfu7/status/1389604687502995457">Video Preview</a> | <a href="https://sli.dev">Documentation</a> | ||
| </p> | ||
| 
     | 
||
| <div align="center"> | ||
| <table> | ||
| <tbody> | ||
| <td align="center"> | ||
| <img width="2000" height="0" alt="" aria-hiden><br> | ||
| <sub>Made possible by my <a href="https://github.com/sponsors/antfu">Sponsor Program 💖</a></sub><br> | ||
| <img width="2000" height="0" alt="" aria-hiden> | ||
| </td> | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| 
     | 
||
| ## Features | ||
| 
     | 
||
| - 📝 [**Markdown-based**](https://sli.dev/guide/syntax) - focus on content and use your favorite editor | ||
| - 🧑💻 [**Developer Friendly**](https://sli.dev/guide/syntax#code-blocks) - built-in code highlighting, live coding, etc. | ||
| - 🎨 [**Themable**](https://sli.dev/resources/theme-gallery) - theme can be shared and used with npm packages | ||
| - 🌈 [**Stylish**](https://sli.dev/guide/syntax#embedded-styles) - on-demand utilities via [UnoCSS](https://github.com/unocss/unocss). | ||
| - 🤹 [**Interactive**](https://sli.dev/custom/directory-structure#components) - embedding Vue components seamlessly | ||
| - 🎙 [**Presenter Mode**](https://sli.dev/guide/ui#presenter-mode) - use another window, or even your phone to control your slides | ||
| - 🎨 [**Drawing**](https://sli.dev/features/drawing) - draw and annotate on your slides | ||
| - 🧮 [**LaTeX**](https://sli.dev/features/latex) - built-in LaTeX math equations support | ||
| - 📰 [**Diagrams**](https://sli.dev/guide/syntax#diagrams) - creates diagrams using textual descriptions with [Mermaid](https://mermaid.js.org/) | ||
| - 🌟 [**Icons**](https://sli.dev/features/icons) - access to icons from any icon set directly | ||
| - 💻 [**Editor**](https://sli.dev/guide/index#editor) - integrated editor, or the [VSCode extension](https://sli.dev/features/vscode-extension) | ||
| - 🎥 [**Recording**](https://sli.dev/features/recording) - built-in recording and camera view | ||
| - 📤 [**Portable**](https://sli.dev/guide/exporting) - export into PDF, PNGs, or PPTX | ||
| - ⚡️ [**Fast**](https://vitejs.dev) - instant reloading powered by [Vite](https://vitejs.dev) | ||
| - 🛠 [**Hackable**](https://sli.dev/custom/) - using Vite plugins, Vue components, or any npm packages | ||
| 
     | 
||
| ## Getting Started | ||
| 
     | 
||
| ### Try it Online ⚡️ | ||
| 
     | 
||
| [sli.dev/new](https://sli.dev/new) | ||
| 
     | 
||
| [](https://sli.dev/new) | ||
| 
     | 
||
| ### Init Project Locally | ||
| 
     | 
||
| Install [Node.js >=18](https://nodejs.org/) and run the following command: | ||
| # 🎬 SlidevVideo Playback Demo | ||
| 
     | 
||
| A simple demo showcasing **multi-step controlled video playback** using `pause` prop. | ||
| 
     | 
||
| 透過 `pause` 參數控制影片分段播放,支援數值與 `'end'`,可用於教學投影片互動控制。 | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ## 🚀 How to Run | ||
| 
     | 
||
| ```bash | ||
| npm init slidev | ||
| pnpm install | ||
| pnpm dev | ||
| ``` | ||
| 
     | 
||
| Documentation: | ||
| **[English](https://sli.dev)** | [中文文档](https://cn.sli.dev) | [Français](https://fr.sli.dev) | [Español](https://es.sli.dev) | [Русский](https://ru.sli.dev) | [Português-BR](https://br.sli.dev) | ||
| Then open [http://localhost:3030](http://localhost:3030) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| Discord: [chat.sli.dev](https://chat.sli.dev) | ||
| ## 🌐 Slide Demos | 投影片展示 | ||
| 
     | 
||
| For a full example, you can check the [demo](https://github.com/slidevjs/slidev/blob/main/demo) folder, which is also the source file for [my previous talk](https://antfu.me/posts/composable-vue-vueday-2021). | ||
| - 👉 [Home Page](/dev.md) | ||
| 
     | 
||
| ## Tech Stack | ||
| --- | ||
| 
     | 
||
| - [Vite](https://vitejs.dev) - An extremely fast frontend tooling | ||
| - [Vue 3](https://v3.vuejs.org/) powered [Markdown](https://daringfireball.net/projects/markdown/syntax) - Focus on the content while having the power of HTML and Vue components whenever needed | ||
| - [UnoCSS](https://github.com/unocss/unocss) - On-demand utility-first CSS engine, style your slides at ease | ||
| - [Shiki](https://github.com/shikijs/shiki), [Monaco Editor](https://github.com/Microsoft/monaco-editor) - First-class code snippets support with live coding capability | ||
| - [RecordRTC](https://recordrtc.org) - Built-in recording and camera view | ||
| - [VueUse](https://vueuse.org) family - [`@vueuse/core`](https://github.com/vueuse/vueuse), [`@vueuse/motion`](https://github.com/vueuse/motion), etc. | ||
| - [Iconify](https://iconify.design/) - Icon sets collection. | ||
| - [Drauu](https://github.com/antfu/drauu) - Drawing and annotations support | ||
| - [KaTeX](https://katex.org/) - LaTeX math rendering. | ||
| - [Mermaid](https://mermaid-js.github.io/mermaid) - Textual Diagrams. | ||
| ## 💡 Example | ||
| 
     | 
||
| ## Sponsors | ||
| ```vue | ||
| <SlidevVideo :pause="[3.5, 2.5, 3]" controls> | ||
| <source src="https://www.w3schools.com/html/mov_bbb.mp4" /> | ||
| </SlidevVideo> | ||
| ``` | ||
| 
     | 
||
| This project is made possible by all the sponsors supporting my work: | ||
| Will play **3.5s**, then pause, then **2.5s**, then pause, then **3s**, then pause, then end. | ||
| 
     | 
||
| <p align="center"> | ||
| <a href="https://github.com/sponsors/antfu"> | ||
| <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg' alt="Logos from Sponsors" /> | ||
| </a> | ||
| </p> | ||
| --- | ||
| 
     | 
||
| ## License | ||
| ## 🧩 Credit | ||
| 
     | 
||
| MIT License © 2021 [Anthony Fu](https://github.com/antfu) | ||
| Based on [`Slidev`](https://github.com/slidevjs/slidev), with custom `SlidevVideo` component. | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # 🎬 SlidevVideo Demo | ||
| 
     | 
||
| > A demo for multi-step controlled playback using `pause=[1, 2, 3, 'end']` | ||
| > 使用 `pause=[1, 2, 3, 'end']` 控制多段播放的展示 | ||
                
       | 
||
| <br> | ||
| <br> | ||
| <br> | ||
| 
     | 
||
| ### 🌐 Choose Language | 選擇語言 | ||
| 
     | 
||
| <Link to="2">English</Link> | ||
| <br> | ||
| <Link to="6">中文</Link> | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| # SlidevVideo 1.0 Test: Pause with Decimal Durations | ||
| 
     | 
||
| This slide demonstrates the new `pause` feature in `SlidevVideo`: | ||
| 
     | 
||
| - ✅ Supports multiple controlled playback segments | ||
| - ✅ Accepts decimal values for pause durations (e.g., `3.5` seconds) | ||
| - ✅ `'end'` is optional — without it, the last segment will stop naturally | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ## Playback Test Example | ||
| 
     | 
||
| <SlidevVideo :pause="[3.5, 2.5, 3]" controls> | ||
| <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" /> | ||
| </SlidevVideo> | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ### ⏱️ Expected Playback Flow | ||
| 
     | 
||
| 1. First click: plays 3.5 seconds, then auto-pauses | ||
| 2. Second click: plays 2.5 seconds, then auto-pauses | ||
| 3. Third click: plays 3 seconds, then auto-pauses | ||
| 4. Fourth click: continues playing until the video ends | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ### Notes | ||
| 
     | 
||
| - `'end'` is optional — if omitted, the last segment just ends playback naturally | ||
| - To force a final uninterrupted play-to-end segment, append `'end'` manually: | ||
| ```html | ||
| <SlidevVideo :pause="[3.5, 2.5, 3, 'end']"></SlidevVideo> | ||
| ``` | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| # 測試 SlidevVideo 1.0:Pause 支援小數點時間 | ||
| 
     | 
||
| 這頁展示了 `SlidevVideo` 新增的 `pause` 支援: | ||
| 
     | 
||
| - ✅ 多段播放控制:每點一次播放指定秒數 | ||
| - ✅ 支援小數點秒數(例如 `3.5` 秒) | ||
| - ✅ 可選擇是否加入 `'end'`,若未加則播放到最後自動停止 | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ## 播放測試範例 | ||
| 
     | 
||
| <SlidevVideo :pause="[3.5, 2.5, 3]" controls> | ||
| <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" /> | ||
| </SlidevVideo> | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ### ⏱️ 播放節奏預期 | ||
| 
     | 
||
| 1. 點第一下:播放 3.5 秒後自動暫停 | ||
| 2. 點第二下:播放 2.5 秒後自動暫停 | ||
| 3. 點第三下:播放 3 秒後自動暫停 | ||
| 4. 第四下:會繼續播放到結尾(或結束整段) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ### 備註 | ||
| 
     | 
||
| - 可以不必加 `'end'`,預設最後一段播放完就不會卡住 | ||
| - 若要強制到最後自動播放到結尾,可明確加 `'end'`: | ||
| ```html | ||
| <SlidevVideo :pause="[3.5, 2.5, 3, 'end']"></SlidevVideo> | ||
| ``` | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,74 +1,111 @@ | ||
| <script setup lang="ts"> | ||
| import { and } from '@vueuse/math' | ||
| import { computed, onMounted, ref, watch } from 'vue' | ||
| import { useNav } from '../composables/useNav' | ||
| import { useSlideContext } from '../context' | ||
| import { resolvedClickMap } from '../modules/v-click' | ||
| import videojs from 'video.js' | ||
| import { computed, onBeforeUnmount, onMounted, ref } from 'vue' | ||
| import 'video.js/dist/video-js.css' | ||
| 
     | 
||
| const props = defineProps<{ | ||
| autoplay?: boolean | 'once' | ||
| autoreset?: 'slide' | 'click' | ||
| pause?: (number | 'end')[] | ||
| poster?: string | ||
| printPoster?: string | ||
| timestamp?: string | number | ||
| printTimestamp?: string | number | 'last' | ||
| controls?: boolean | ||
| }>() | ||
| 
     | 
||
| const printPoster = computed(() => props.printPoster ?? props.poster) | ||
| const printTimestamp = computed(() => props.printTimestamp ?? props.timestamp ?? 0) | ||
| const videoRef = ref() | ||
| const player = ref<ReturnType<typeof videojs> | null>(null) | ||
| const currentInterval = ref<any>(null) | ||
| 
     | 
||
| const { $slidev, $renderContext, $route } = useSlideContext() | ||
| const { isPrintMode } = useNav() | ||
| const rawPause = props.pause ?? [] | ||
| const pauseTimestamps = computed(() => { | ||
| const out: (number | 'end')[] = [0] | ||
| for (const segment of rawPause) { | ||
| const last = out[out.length - 1] | ||
| if (segment === 'end') { | ||
| out.push('end') | ||
| } | ||
| else { | ||
| const lastNum = typeof last === 'number' ? last : 0 | ||
| out.push(lastNum + segment) | ||
| } | ||
| } | ||
| return out | ||
| }) | ||
| 
     | 
||
| const noPlay = computed(() => isPrintMode.value || !['slide', 'presenter'].includes($renderContext.value)) | ||
| const pauseIndex = ref(1) | ||
| const isPlaying = ref(false) | ||
| 
     | 
||
| const video = ref<HTMLMediaElement>() | ||
| const played = ref(false) | ||
| function playNextSegment() { | ||
| const from = pauseTimestamps.value[pauseIndex.value - 1] | ||
| const to = pauseTimestamps.value[pauseIndex.value] | ||
| 
     | 
||
| onMounted(() => { | ||
| if (noPlay.value) | ||
| if (!player.value || from == null || to == null) | ||
| return | ||
| 
     | 
||
| const timestamp = +(props.timestamp ?? 0) | ||
| video.value!.currentTime = timestamp | ||
| if (typeof from === 'number') { | ||
| player.value.currentTime(from) | ||
| } | ||
| 
     | 
||
| const matchRoute = computed(() => !!$route && $route.no === $slidev?.nav.currentSlideNo) | ||
| const matchClick = computed(() => !!video.value && (resolvedClickMap.get(video.value)?.isShown?.value ?? true)) | ||
| const matchRouteAndClick = and(matchRoute, matchClick) | ||
| try { | ||
| isPlaying.value = true | ||
| player.value.play() | ||
| } | ||
| catch { | ||
| isPlaying.value = false | ||
| return | ||
| } | ||
| 
     | 
||
| watch(matchRouteAndClick, () => { | ||
| if (matchRouteAndClick.value) { | ||
| if (props.autoplay === true || (props.autoplay === 'once' && !played.value)) | ||
| video.value!.play() | ||
| } | ||
| else { | ||
| video.value!.pause() | ||
| if (props.autoreset === 'click' || (props.autoreset === 'slide' && !matchRoute.value)) | ||
| video.value!.currentTime = timestamp | ||
| if (to === 'end') { | ||
| pauseIndex.value++ | ||
| return | ||
| } | ||
| 
     | 
||
| if (currentInterval.value) | ||
| clearInterval(currentInterval.value) | ||
| 
     | 
||
| currentInterval.value = setInterval(() => { | ||
| if (!player.value) | ||
| return | ||
| if (player.value.currentTime() >= to) { | ||
| player.value.pause() | ||
| clearInterval(currentInterval.value) | ||
| isPlaying.value = false | ||
| pauseIndex.value++ | ||
| } | ||
| }, { immediate: true }) | ||
| }, 100) | ||
| } | ||
| 
     | 
||
| onMounted(() => { | ||
| player.value = videojs(videoRef.value, { | ||
| controls: props.controls !== false, | ||
| autoplay: false, | ||
| preload: 'auto', | ||
| poster: props.poster, | ||
| }) | ||
| 
     | 
||
| player.value.ready(() => { | ||
| player.value.controls(true) | ||
| player.value.trigger('resize') | ||
| player.value.pause() | ||
| player.value.currentTime(0) | ||
| 
     | 
||
| player.value.on('play', () => { | ||
| if (!isPlaying.value) { | ||
| playNextSegment() | ||
| } | ||
| else { | ||
| player.value?.pause() | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
| 
     | 
||
| function onLoadedMetadata(ev: Event) { | ||
| // The video may be loaded before component mounted | ||
| const element = ev.target as HTMLMediaElement | ||
| if (noPlay.value && (!printPoster.value || props.printTimestamp)) { | ||
| element.currentTime = printTimestamp.value === 'last' | ||
| ? element.duration | ||
| : +printTimestamp.value | ||
| } | ||
| } | ||
| onBeforeUnmount(() => { | ||
| if (currentInterval.value) | ||
| clearInterval(currentInterval.value) | ||
| player.value?.dispose() | ||
| }) | ||
| </script> | ||
| 
     | 
||
| <template> | ||
| <video | ||
| ref="video" | ||
| :poster="noPlay ? printPoster : props.poster" | ||
| :controls="!noPlay && props.controls" | ||
| @play="played = true" | ||
| @loadedmetadata="onLoadedMetadata" | ||
| > | ||
| <video ref="videoRef" class="video-js vjs-default-skin" playsinline> | ||
| <slot /> | ||
| </video> | ||
| </template> | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this file has been changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Anthony, thanks for the feedback!
I thought I was only editing my fork, so I put the demo into the README for quick testing.
Now I understand it should go into the docs and in English.
I’ll move the content into the docs and update the PR. Thanks for pointing it out!