refactor: refactor volume control sync
This commit is contained in:
parent
94464dcdf2
commit
778a54db1f
|
@ -29,6 +29,7 @@ const EpisodePlayer = ({
|
||||||
const playerContainerRef = useRef<HTMLDivElement>(null)
|
const playerContainerRef = useRef<HTMLDivElement>(null)
|
||||||
const playerRef = useRef<ReactPlayer>(null)
|
const playerRef = useRef<ReactPlayer>(null)
|
||||||
|
|
||||||
|
const [volume, setVolume] = useState(state.value.volume)
|
||||||
const isSimplecast = channel?.name === LPE.Podcast.ChannelNames.Simplecast
|
const isSimplecast = channel?.name === LPE.Podcast.ChannelNames.Simplecast
|
||||||
|
|
||||||
const url =
|
const url =
|
||||||
|
@ -71,6 +72,7 @@ const EpisodePlayer = ({
|
||||||
state.set((prev) => ({
|
state.set((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
isEnabled: true,
|
isEnabled: true,
|
||||||
|
volume: volume,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -79,7 +81,7 @@ const EpisodePlayer = ({
|
||||||
return () => {
|
return () => {
|
||||||
observer.disconnect()
|
observer.disconnect()
|
||||||
}
|
}
|
||||||
}, [keepGlobalPlay])
|
}, [keepGlobalPlay, volume])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleLeave = () => {
|
const handleLeave = () => {
|
||||||
|
@ -114,17 +116,41 @@ const EpisodePlayer = ({
|
||||||
}, [state.value.isEnabled])
|
}, [state.value.isEnabled])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (channel?.name === LPE.Podcast.ChannelNames.Youtube) {
|
const listener = (event: any) => {
|
||||||
window.addEventListener('message', function (event) {
|
if (event.origin == 'https://www.youtube.com') {
|
||||||
if (event.origin == 'https://www.youtube.com') {
|
const data = JSON.parse(event?.data)
|
||||||
const data = JSON.parse(event?.data)
|
|
||||||
const volume = data?.info?.volume
|
|
||||||
|
|
||||||
if (typeof volume !== 'undefined') {
|
if (data?.info?.hasOwnProperty('muted') && !state.value.isEnabled) {
|
||||||
state.set((prev) => ({ ...prev, volume: volume / 100 }))
|
const isMuted = data.info.muted === true
|
||||||
|
const muteChanged = state.value.muted !== isMuted
|
||||||
|
|
||||||
|
const newVolume = data.info?.volume / 100 || state.value.volume
|
||||||
|
|
||||||
|
if (isMuted) {
|
||||||
|
if (muteChanged) {
|
||||||
|
state.set((prev) => ({ ...prev, muted: true, volume: newVolume }))
|
||||||
|
}
|
||||||
|
} else if (!isMuted) {
|
||||||
|
if (muteChanged) {
|
||||||
|
state.set((prev) => ({
|
||||||
|
...prev,
|
||||||
|
muted: false,
|
||||||
|
volume: newVolume,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
setVolume(newVolume)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel?.name === LPE.Podcast.ChannelNames.Youtube) {
|
||||||
|
window.addEventListener('message', listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', listener)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue