audio video播放 组件控制

audio

	const AudioCom = () => {
		const audioRef = useRef<any>();
    	const [isPlay, setIsPlay] = useState(false);

    const listenCanPlayAudio = () => {
        if (audioRef.current) {
            audioRef.current.addEventListener('canplay', () => {
                audioRef.current.play();
                // setTimeout(() => {
                //     audioRef.current.play();
                // }, 100);
            });
        }
    };

    // 音乐播放
    const playMusic = () => {
        if (audioRef.current) {
            audioRef.current.play();
            try {
                const windowObj = window.windowObj || {};
                // 微信兼容
                if (windowObj.wx) {
                    windowObj.wx.ready(() => {
                        audioRef.current.play();
                    });
                }
            } catch (error) {}
        }
    };

    // 音乐暂停
    const pauseMusic = () => {
        if (audioRef.current) {
            audioRef.current.pause();
        }
    };

    // 音乐播放状态
    const listenMusicPlayStatus = () => {
        if (audioRef.current) {
            audioRef.current.addEventListener('play', () => {
                setIsPlay(true);
            });

            audioRef.current.addEventListener('pause', () => {
                setIsPlay(false);
            });
        }
    };

    // 点击音乐图标
    const clickMusicIcon = () => {
        if (isPlay) {
            // 播放中
            pauseMusic();
        } else {
            // 暂停
            playMusic();
        }
    };

    useEffect(() => {
        listenCanPlayAudio();
        listenMusicPlayStatus();
    }, []);

return  (<div>
	 		<div 
	 		onClick={() => {clickMusicIcon();}}
                className={isPlay ? styles.bgm : styles.bgmc}
            ></div>
        	<audio className={styles.audio} ref={audioRef} loop preload="preload" src={BGMUrl}></audio>
		</div>)
	}

export default AudioCom 
  

video

	import React, { useEffect, useRef, useState } from 'react';
	import './index.less';
	import videoUrl from 'src/assets/video.mp4';

const Loading = ({ onLoaded }) => {
    const loaddingRef = useRef<any>(null);
    const endedCallbackRef = useRef<any>(null);
    const [isOver, setIsOver] = useState(false);
    const timer = useRef<any>(); // 避免 ios 低电量下无法自动播放回调 设置回调时间

    useEffect(() => {
        const onCallback = () => {
            endedCallbackRef?.current?.();
            setIsOver(true);
        };

        if (loaddingRef?.current && loaddingRef?.current?.addEventListener) {
            loaddingRef?.current?.addEventListener?.('ended', onCallback);
        }
        return () => {
            loaddingRef?.current?.removeEventListener?.('ended', onCallback);
        };
    }, []);

    useEffect(() => {
            setIsOver(false);
            endedCallbackRef.current = onLoaded;
            loaddingRef.current.currentTime = 0;
            loaddingRef.current.play();
            timer.current = setTimeout(() => {
                console.log('倒计时2S 播放结束');
                setIsOver(true);
                onLoaded();
            }, 2000);
        
        return () => {
            clearTimeout(timer.current);
        };
    }, [onLoaded]);

    useEffect(() => {
        loaddingRef.current.currentTime = 0;
        loaddingRef.current.play();
    }, []);

    return (
        <div className="screenBox-loading" style={{ zIndex: `${isOver ? -1 : 99}` }}>
            <video
                className="screenBoxVideo"
                style={{ zIndex: `${isOver ? -1 : 99}` }}
                // 设定播放完后放入后面
                ref={loaddingRef}
                muted
                loop={false}
                playsInline
                poster={videoUrl}
            >
                <source src={videoUrl} type="video/mp4" />
            </video>
        </div>
    );
};

export default Loading;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值