我将我的代码重构更加干净和重复使用。但是,在我重构代码后,功能/功能之一无效

发布于 2025-01-22 00:40:05 字数 2054 浏览 1 评论 0原文

大家好,我正在更加干净,可重复使用。这是我想做的。
  • 访问“登录”页面时,登录。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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2025-01-29 00:40:05

它不起作用,因为分配了新音频后,您会失去对旧音频的引用。
首先暂停,然后分配新声音

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文