feat: audio
This commit is contained in:
64
src/services/media-manager.service.ts
Normal file
64
src/services/media-manager.service.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
class MediaManagerService {
|
||||
static instance: MediaManagerService
|
||||
|
||||
private currentMedia: HTMLMediaElement | null = null
|
||||
|
||||
constructor() {
|
||||
if (!MediaManagerService.instance) {
|
||||
MediaManagerService.instance = this
|
||||
}
|
||||
return MediaManagerService.instance
|
||||
}
|
||||
|
||||
pause(media: HTMLMediaElement) {
|
||||
if (isPipElement(media)) {
|
||||
return
|
||||
}
|
||||
if (this.currentMedia === media) {
|
||||
this.currentMedia = null
|
||||
}
|
||||
media.pause()
|
||||
}
|
||||
|
||||
autoPlay(media: HTMLMediaElement) {
|
||||
if (
|
||||
document.pictureInPictureElement &&
|
||||
isMediaPlaying(document.pictureInPictureElement as HTMLMediaElement)
|
||||
) {
|
||||
return
|
||||
}
|
||||
this.play(media)
|
||||
}
|
||||
|
||||
play(media: HTMLMediaElement) {
|
||||
if (document.pictureInPictureElement && document.pictureInPictureElement !== media) {
|
||||
;(document.pictureInPictureElement as HTMLMediaElement).pause()
|
||||
}
|
||||
if (this.currentMedia && this.currentMedia !== media) {
|
||||
this.currentMedia.pause()
|
||||
}
|
||||
this.currentMedia = media
|
||||
if (isMediaPlaying(media)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.currentMedia.play().catch((error) => {
|
||||
console.error('Error playing media:', error)
|
||||
this.currentMedia = null
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new MediaManagerService()
|
||||
export default instance
|
||||
|
||||
function isMediaPlaying(media: HTMLMediaElement) {
|
||||
return media.currentTime > 0 && !media.paused && !media.ended && media.readyState >= 2
|
||||
}
|
||||
|
||||
function isPipElement(media: HTMLMediaElement) {
|
||||
if (document.pictureInPictureElement === media) {
|
||||
return true
|
||||
}
|
||||
return (media as any).webkitPresentationMode === 'picture-in-picture'
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
class VideoManagerService {
|
||||
static instance: VideoManagerService
|
||||
|
||||
private currentVideo: HTMLVideoElement | null = null
|
||||
|
||||
constructor() {
|
||||
if (!VideoManagerService.instance) {
|
||||
VideoManagerService.instance = this
|
||||
}
|
||||
return VideoManagerService.instance
|
||||
}
|
||||
|
||||
pause(video: HTMLVideoElement) {
|
||||
if (isPipElement(video)) {
|
||||
return
|
||||
}
|
||||
if (this.currentVideo === video) {
|
||||
this.currentVideo = null
|
||||
}
|
||||
video.pause()
|
||||
}
|
||||
|
||||
autoPlay(video: HTMLVideoElement) {
|
||||
if (
|
||||
document.pictureInPictureElement &&
|
||||
isVideoPlaying(document.pictureInPictureElement as HTMLVideoElement)
|
||||
) {
|
||||
return
|
||||
}
|
||||
this.play(video)
|
||||
}
|
||||
|
||||
play(video: HTMLVideoElement) {
|
||||
if (document.pictureInPictureElement && document.pictureInPictureElement !== video) {
|
||||
;(document.pictureInPictureElement as HTMLVideoElement).pause()
|
||||
}
|
||||
if (this.currentVideo && this.currentVideo !== video) {
|
||||
this.currentVideo.pause()
|
||||
}
|
||||
this.currentVideo = video
|
||||
if (isVideoPlaying(video)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.currentVideo.play().catch((error) => {
|
||||
console.error('Error playing video:', error)
|
||||
this.currentVideo = null
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new VideoManagerService()
|
||||
export default instance
|
||||
|
||||
function isVideoPlaying(video: HTMLVideoElement) {
|
||||
return video.currentTime > 0 && !video.paused && !video.ended && video.readyState >= 2
|
||||
}
|
||||
|
||||
function isPipElement(video: HTMLVideoElement) {
|
||||
if (document.pictureInPictureElement === video) {
|
||||
return true
|
||||
}
|
||||
return (video as any).webkitPresentationMode === 'picture-in-picture'
|
||||
}
|
||||
Reference in New Issue
Block a user