Home>Article>Web Front-end> How to customize the video player in Angular
How to customize Video operations? Custom video player? The following article will introduce to you how to customize Video operations inAngular. I hope it will be helpful to you!
The previous article wasAngular project implementation of permission control. Recently, I saw others usingvue
on the Internet to customizevideo
. In addition, the related requirements ofangular
customizationvideo
were implemented not long ago, so I will record it as a communication and reflection. [Related tutorial recommendations: "angular tutorial"]
The functions implemented are as follows:
As shown in the picture:
Let’s implement it one by one:
The focus here is not on the layout, let’s simply define it:
- 快进 10 s
- 快进 20 s
- 快退 10 s
- 快退 20 s
- 正常
- 2 倍
- 4 倍
经过时长 / 总时长 : ✅ {{ currentTime }} / {{ totalTime }}进度条:✅声音条:✅
angular ant design
is used here. I wrote a related article before. Readers who are not familiar with it can go toAngular combined with NG-ZORRO rapid development
Here directly call thevideo
object methodsplay()
andpause()
:
// 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 }
The customizedplay
andpause
methods here add a flag, which is helpful for controlling the progress bar to be discussed below. The above code can be more concise , readers can write it down in abbreviation.
HereFast rewind, fast forward and double speedset different options and pass them through parameters:
// 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 }
To switch the sound on and off, use themuted
attribute ofvideo
:
// app.component.ts // 开或关声音 openOrCloseVoice(): void { this.video.muted = !this.video.muted; }
The operation of full screen is also very simple. UsewebkitRequestFullScreen
// app.component.ts // 全屏操作 toFullScreen(): void { this.video.webkitRequestFullScreen() }
After full screen, press
esc
Exit full screen
Picture-in-picture is equivalent to pop-up window shrink video~
// 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" } }
Settingsvideo## The style of # is to not look obtrusive...
// 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 is the formatting functionPlayback progress bar function Monitor the mouse
click, move, releaseEvents, divide the video play time and total events to calculate the percentage.
// 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) } }Here you need to calculate the position of the progress bar to get the percentage of clicks on the progress bar, and then update the current playback time of the video. Of course, we must also have fault tolerance processing. For example, when the progress bar is negative, the current playback time is 0. Sound progress bar We have implemented the operation of the playback progress bar, and it is easy to get started with the implementation of the sound progress bar. The sound progress bar also monitors mouse
clicks, moves, and releases. However, this time we are dealing with the height of the known sounddiv.
// 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); // 赋值给视频声音 }Picture: Effect demonstration After completing the above content, we use a
gifpicture To show the effect:
Full screen, sound and picture-in-picture are difficult to capture, andGif
cannot be reflected on
For detailed code, please go tovideo-ngto get it.
【End】
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of How to customize the video player in Angular. For more information, please follow other related articles on the PHP Chinese website!