From 089654a33874f32f4d313a8b4b3af5077fcd0e59 Mon Sep 17 00:00:00 2001 From: Bryan Rodriguez Date: Tue, 5 Mar 2024 11:43:27 -0500 Subject: [PATCH] 2024.2.1 : volumen save function --- src/Context/PlayerContext.jsx | 57 +++++++++-------------------------- src/Music.jsx | 11 +++++-- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/Context/PlayerContext.jsx b/src/Context/PlayerContext.jsx index 5437baf..08d5b34 100644 --- a/src/Context/PlayerContext.jsx +++ b/src/Context/PlayerContext.jsx @@ -28,20 +28,22 @@ export const PlayerProvider = ({ children }) => { } }); + //LOAD useEffect(() => { const savedData = localStorage.getItem('videoPlayer'); + const savedVolume = localStorage.getItem('volume'); if (savedData) { setVideoPlayer(JSON.parse(savedData)); + setVolume(parseFloat(savedVolume)); } }, []); + //SAVE useEffect(() => { localStorage.setItem('videoPlayer', JSON.stringify(videoPlayer)); - const savedUrl = localStorage.getItem('url'); - if (savedUrl) { - setUrl(savedUrl); - } - }, [videoPlayer]); + localStorage.setItem('volume', JSON.stringify(volume)); + localStorage.setItem('url', url); + }, [videoPlayer, volume, url]); // useEffect(() => { // const savedData = localStorage.getItem('videoPlayer'); @@ -94,16 +96,13 @@ export const PlayerProvider = ({ children }) => { })); }; - // const handleVolumeChange = (value) => { - // setVideoPlayer((prevPlayer) => ({ - // ...prevPlayer, - // volume: value, - // })); - // setVolume(value); - // }; - const handleVolumeChange = (event) => { - setVolume(event.target.value); + const newVolume = parseFloat(event.target.value); + setVolume(newVolume); + setVideoPlayer((prevPlayer) => ({ + ...prevPlayer, + volume: newVolume // Guardar el volumen en el objeto videoPlayer + })); }; const handleMute = () => { @@ -111,8 +110,8 @@ export const PlayerProvider = ({ children }) => { ...prevPlayer, muted: !prevPlayer.muted, }) - ); - setMuted(!muted); + ); + setMuted(!muted); }; const handleSaveProgress = () => { @@ -145,32 +144,6 @@ export const PlayerProvider = ({ children }) => { }; - // const handleProgress = (progress) => { - // setPlayed(progress.played); - // }; - - // const handleDuration = (duration) => { - // setDuration(duration); - // }; - - // const handleVolumeChange = (value) => { - // setVolume(value); - // }; - - - // const handleSaveProgress = () => { - // const currentTime = localStorage.getItem('time'); - - // if (currentTime !== played.toString()) { - // localStorage.setItem('time', played.toString()); - // } - // }; - - // const handleSaveUrl = (newUrl) => { - // localStorage.setItem('url', newUrl); - // setUrl(newUrl); - // }; - const handleInputChange = (event) => { const newUrl = event.target.value; handleSaveUrl(newUrl); diff --git a/src/Music.jsx b/src/Music.jsx index 4df7e49..0e6b686 100644 --- a/src/Music.jsx +++ b/src/Music.jsx @@ -1,5 +1,5 @@ -import { useContext } from 'react'; +import { useContext, useState } from 'react'; //import YouTube from 'react-youtube'; //import ReactPlayer from 'react-player/lazy'; import ReactPlayer from 'react-player'; @@ -40,7 +40,12 @@ export function Music() { handleSaveProgress(); }; - console.log(videoPlayer.ShowPip); + const [videoPlayerx, setVideoPlayerx] = useState(() => { + const storedPlayer = JSON.parse(localStorage.getItem('videoPlayer')); + return storedPlayer || []; + }); + + console.log(videoPlayerx.ShowPip); return (
@@ -104,7 +109,7 @@ export function Music() {
- +

100