怎么自定义 Video 操作?自定义视频播放器?下面本篇文章给大家介绍一下Angular 中自定义 Video 操作的方法,希望对大家有所帮助!
上一篇文章是 Angular 项目实现权限控制。最近自己在网上看到别人使用 vue
进行自定义 video
的操作。加上不久前实现了 angular
自定义 video
的相关需求, 遂来记录一下,作为交流思考。【相关教程推荐:《angular教程》】
实现的功能如下:
- 播放 / 停止
- 快退 / 快进 / 倍速
- 声音开 / 声音关
- 进入全屏 / 退出全屏
- 进入画中画 / 退出画中画 【安卓平板不支持,不建议使用】
- 经过时长 / 总时长
- 播放进度条功能:支持点击,拖拽进度
- 声音进度条功能:支持点击,拖拽进度
如图:
下面我们来一一实现:
这里的重点不在布局,我们简单来定义一下:
<!-- app.component.html --> <div class="video-page"> <div class="video-tools"> <button nz-button nzType="primary" (click)="play('btn')" style="margin-right: 12px;">播放 ✅</button> <button nz-button nzType="primary" (click)="pause('btn')">暂停 ✅</button> <ng-container> <button nz-button nz-dropdown [nzDropdownMenu]="menuForward" nzPlacement="bottomCenter" style="margin: 0 12px;">快进 ✅</button> <nz-dropdown-menu #menuForward="nzDropdownMenu"> <ul nz-menu> <li nz-menu-item (click)="forwardSecond(10)">快进 10 s</li> <li nz-menu-item (click)="forwardSecond(20)">快进 20 s</li> </ul> </nz-dropdown-menu> </ng-container> <ng-container> <button nz-button nz-dropdown [nzDropdownMenu]="menuBack" nzPlacement="bottomCenter">快退 ✅</button> <nz-dropdown-menu #menuBack="nzDropdownMenu"> <ul nz-menu> <li nz-menu-item (click)="retreatSecond(10)">快退 10 s</li> <li nz-menu-item (click)="retreatSecond(20)">快退 20 s</li> </ul> </nz-dropdown-menu> </ng-container> <ng-container> <button nz-button nz-dropdown [nzDropdownMenu]="speedUp" nzPlacement="bottomCenter" style="margin: 0 12px;">倍速 ✅</button> <nz-dropdown-menu #speedUp="nzDropdownMenu"> <ul nz-menu> <li nz-menu-item (click)="speedUpVideo(1)">正常</li> <li nz-menu-item (click)="speedUpVideo(2)">2 倍</li> <li nz-menu-item (click)="speedUpVideo(4)">4 倍</li> </ul> </nz-dropdown-menu> </ng-container> <button nz-button nzType="primary" (click)="openOrCloseVoice()">声音开 / 声音关 ✅</button> <button nz-button nzType="primary" style="margin: 0 12px;" (click)="toFullScreen()">全屏 ✅</button> <br /> <button nz-button nzType="primary" style="margin-top: 12px;" (click)="entryInPicture()">进入画中画 ⚠️ 安卓平板不支持</button> <button nz-button nzType="primary" style="margin: 12px 12px 0 12px;" (click)="exitInPicture()">退出画中画 ⚠️ 安卓平板不支持</button> <br /> <div style="display: flex; justify-content: flex-start; align-items: center; margin: 12px 0;"> 经过时长 / 总时长 : ✅ {{ currentTime }} / {{ totalTime }} </div> <!-- 进度条 --> <div style="display: flex; justify-content: flex-start; align-items: center; margin: 12px 0;"> 进度条:✅ <div class="custom-video_control-bg" (mousedown)="handleProgressDown($event)" (mousemove)="handleProgressMove($event)" (mouseup)="handleProgressUp($event)" > <div class="custom-video_control-bg-outside" id="custom-video_control-bg-outside" > <span class="custom-video_control-bg-inside" id="custom-video_control-bg-inside" ></span> <span class="custom-video_control-bg-inside-point" id="custom-video_control-bg-inside-point" ></span> </div> </div> </div> <div style="display: flex; justify-content: flex-start; align-items: center; margin: 12px 0;"> 声音条:✅ <div class="custom-video_control-voice"> <span class="custom-video_control-voice-play"> <i nz-icon nzType="sound" nzTheme="outline"></i> </span> <div class="custom-video_control-voice-bg" id="custom-video_control-voice-bg" (mousedown)="handleVolProgressDown($event)" (mousemove)="handleVolProgressMove($event)" (mouseup)="handleVolProgressUp($event)" > <div class="custom-video_control-voice-bg-outside" id="custom-video_control-voice-bg-outside" > <span class="custom-video_control-voice-bg-inside" id="custom-video_control-voice-bg-inside" ></span> <span class="custom-video_control-voice-bg-point" id="custom-video_control-voice-bg-point" ></span> </div> </div> </div> </div> </div> <div class="video-content"> <video id="video" class="video" style="width: 100%" poster="assets/poster.png"> <source type="video/mp4" src="assets/demo.mp4"> Sorry, your browser doesn't support. </video> </div> </div>
这里使用了
angular ant design
,之前写了一篇相关文章,还不熟悉的读者可前往 Angular 结合 NG-ZORRO 快速开发
播放 / 停止
这里直接调用 video
对象的方法 play()
和 pause()
:
// app.component.ts // 播放按钮事件 play(flag: string | undefined) { if(flag) this.videoState.playState = true this.videoState.play = true this.video.play() } // 暂停按钮事件 pause(flag: string | undefined): void { if(flag) this.videoState.playState = false this.video.pause() this.videoState.play = false }
这里自定义的 play
和 pause
方法加上了一个标志,对下下面要讲的进度条的控制有帮助,上面的代码可以更加简洁,读者可以简写下。
快退 / 快进 / 倍速
这里的快退,快进和倍速设置了不同的选项,通过参数进行传递:
// app.component.ts // 快进指定的时间 forwardSecond(second: number): void { this.video.currentTime += second; // 定位到当前的播放时间 currentTime } // 后退指定的时间 retreatSecond(second: number): void { this.video.currentTime -= second } // 倍速 speedUpVideo(multiple: number): void { this.video.playbackRate = multiple; // 设定当前的倍速 playbackRate }
声音开 / 声音关
声音的开关使用 video
的 muted
属性即可:
// app.component.ts // 开或关声音 openOrCloseVoice(): void { this.video.muted = !this.video.muted; }
进入全屏 / 退出全屏
全屏的操作也是很简单,使用 webkitRequestFullScreen
// app.component.ts // 全屏操作 toFullScreen(): void { this.video.webkitRequestFullScreen() }
全屏后,按
esc
可退出全屏
进入画中画 / 退出画中画
画中画相当于弹窗缩小视频~
// app.component.ts // 进入画中画 entryInPicture(): void { this.video.requestPictureInPicture() this.video.style.display = "none" } // 退出画中画 exitInPicture(): void { if(this.document.pictureInPictureElement) { this.document.exitPictureInPicture() this.video.style.display = "block" } }
设置 video
的样式,是为了看起来不突兀...
经过时长 / 总时长
记录视频的总时长和视频当前的播放时长。我们已经来组件的时候就获取视频的元信息,得到总时长;在视频播放的过程中,更新当前时长。
// app.component.ts // 初始化 video 的相关的事件 initVideoData(): void { // 获取视频的总时长 this.video.addEventListener('loadedmetadata', () => { this.totalTime = this.formatTime(this.video.duration) }) // 监听时间发生更改 this.video.addEventListener('timeupdate', () => { this.currentTime = this.formatTime(this.video.currentTime) // 当前播放的时间 }) }
formatTime 是格式化函数
播放进度条功能
监听鼠标的点击,移动,松开的事件,对视频的播放时间和总事件进行相除,计算百分比。
// app.component.ts // 进度条鼠标按下 handleProgressDown(event: any): void { this.videoState.downState = true this.pause(undefined); this.videoState.distance = event.clientX + document.documentElement.scrollLeft - this.videoState.leftInit; } // 进度条 滚动条移动 handleProgressMove(event: any): void { if(!this.videoState.downState) return let distanceX = (event.clientX + document.documentElement.scrollLeft) - this.videoState.leftInit if(distanceX > this.processWidth) { // 容错处理 distanceX = this.processWidth; } if(distanceX < 0) { // 容错处理 distanceX = 0 } this.videoState.distance = distanceX this.video.currentTime = this.videoState.distance / this.processWidth * this.video.duration } // 进度条 鼠标抬起 handleProgressUp(event: any): void { this.videoState.downState = false // 视频播放 this.video.currentTime = this.videoState.distance / this.processWidth * this.video.duration this.currentTime = this.formatTime(this.video.currentTime) if(this.videoState.playState) { this.play(undefined) } }
这里需要计算进度条的位置,来获取点击进度条的百分比,之后更新视频的当前播放时间。当然,我们还得有容错处理,比如进度条为负数时候,当前播放时间为0。
声音进度条
我们实现了播放进度条的操作,对声音进度条的实现就很容易上手了。声音进度条也是监听鼠标的点击,移动,松开。不过,这次我们处理的是已知声音 div
的高度。
// app.component.ts // 声音条 鼠标按下 handleVolProgressDown(event: any) { this.voiceState.topInit = this.getOffset(this.voiceProOut, undefined).top this.volProcessHeight = this.voiceProOut.clientHeight this.voiceState.downState = true //按下鼠标标志 this.voiceState.distance = this.volProcessHeight - (event.clientY + document.documentElement.scrollTop - this.voiceState.topInit) } // 声音 滚动条移动 handleVolProgressMove(event: any) { if(!this.voiceState.downState) return let disY = this.voiceState.topInit + this.volProcessHeight - (event.clientY + document.documentElement.scrollTop) if(disY > this.volProcessHeight - 2) { // 容错处理 disY = this.volProcessHeight - 2 } if(disY < 0) { // 容错处理 disY = 0 } this.voiceState.distance = disY this.video.volume = this.voiceState.distance / this.volProcessHeight this.videoOption.volume = Math.round(this.video.volume * 100) } // 声音 鼠标抬起 handleVolProgressUp(event: any) { this.voiceState.downState = false //按下鼠标标志 let voiceRate = this.voiceState.distance / this.volProcessHeight if(voiceRate > 1) { voiceRate = 1 } if(voiceRate < 0) { voiceRate = 0 } this.video.volume = voiceRate this.videoOption.volume = Math.round(this.video.volume * 100); // 赋值给视频声音 }
如图:
效果演示
完成了上面的内容,我们以一个 gif
图来展示效果:
全屏,声音和画中画比较难截图,
Gif
上体现不来
详细的代码,请前往 video-ng 获取。
【完】
更多编程相关知识,请访问:编程入门!!
以上是Angular中怎么自定义视频播放器的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库

JavaScript是现代Web开发的核心语言,因其多样性和灵活性而广泛应用。1)前端开发:通过DOM操作和现代框架(如React、Vue.js、Angular)构建动态网页和单页面应用。2)服务器端开发:Node.js利用非阻塞I/O模型处理高并发和实时应用。3)移动和桌面应用开发:通过ReactNative和Electron实现跨平台开发,提高开发效率。

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。 1.Python以简洁语法和丰富库生态着称,适用于数据分析和Web开发。 2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

JavaScript不需要安装,因为它已内置于现代浏览器中。你只需文本编辑器和浏览器即可开始使用。1)在浏览器环境中,通过标签嵌入HTML文件中运行。2)在Node.js环境中,下载并安装Node.js后,通过命令行运行JavaScript文件。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能