我将我的代码重构更加干净和重复使用。但是,在我重构代码后,功能/功能之一无效
大家好,我正在更加干净,可重复使用。这是我想做的。
访问“登录”页面时,登录。mp3在页面上自动播放。 (“ PLZ输入您的电话号码~~”)
如果在屏幕上有任何单击事件/动作,音乐会停止播放。
当您移动“付款”页面时,付款.mp3文件会自动播放。
如果屏幕上有任何单击事件/动作,音乐会停止播放。
我希望你们能理解我想做的什么
,但是在我重构代码后,“停止播放音乐”功能不起作用。当我在窗口上单击事件时,应该可以工作。你们能帮我如何解决吗?在
重构
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const [isPlaying, setIsPlaying] = useState(true);
const audioFile = new Audio(`/sounds/hello.mp3`);
// play audio sound
const playSound = () => {
audioFile.play();
};
// stop audio sound
const stopSound = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
useEffect(() => {
playSound();
// when there's click event on window, it stops playing the music
const clickHandler = () => stopSound();
window.addEventListener('click', clickHandler);
return () => window.removeEventListener('click', clickHandler);
}, []);
;
LoginPhone.TSX
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const {playAudio, stopAudio} = voiceAudio('login-phone');
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
Utils&gt voice.ts
export const voiceAudio = (title: string) => {
const audioFile = new Audio(`/sounds/${title}.mp3`);
const playAudio = () => audioFile.play();
const stopAudio = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
return {
playAudio,
stopAudio,
};
};
Hey guys, I'm refactoring my code more clean and reusable. Here's what I would like to make.
When you visit 'login' page, login.mp3 plays automatically on the page. ("plz enter your phone number~~")
if there's any click event/action on the screen, the music stops playing.
When you move to 'payment' page, payment.mp3 file plays automatically.
if there's any click event/action on the screen, the music stops playing.
I hope you guys could understand what I'm trying to make
However after I refactored the code, 'stop playing music' feature doesn't work. It's supposed to work when I make click event on window. Can you guys help me how I can fix it? Thanks
Before Refactored
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const [isPlaying, setIsPlaying] = useState(true);
const audioFile = new Audio(`/sounds/hello.mp3`);
// play audio sound
const playSound = () => {
audioFile.play();
};
// stop audio sound
const stopSound = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
useEffect(() => {
playSound();
// when there's click event on window, it stops playing the music
const clickHandler = () => stopSound();
window.addEventListener('click', clickHandler);
return () => window.removeEventListener('click', clickHandler);
}, []);
After Refactored
loginPhone.tsx
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const {playAudio, stopAudio} = voiceAudio('login-phone');
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
utils > voice.ts
export const voiceAudio = (title: string) => {
const audioFile = new Audio(`/sounds/${title}.mp3`);
const playAudio = () => audioFile.play();
const stopAudio = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
return {
playAudio,
stopAudio,
};
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为分配了新音频后,您会失去对旧音频的引用。
首先暂停,然后分配新声音
It doesn't work because you are losing a reference to the old audio after assigning a new one.
First pause, then assign new sound