feat(video): 添加阿里云播放器自定义属性支持和实例方法

- 添加 aliPlayerProps 属性用于传递自定义参数给 AliPlayer
- 在 AliPlayer 组件中暴露 getPlayerInstance 方法获取播放器实例
- 将 Video 组件改为 forwardRef 以支持外部引用
master
LiuJiaNan 2026-04-02 11:28:51 +08:00
parent f501c9683a
commit a8b04b3fba
4 changed files with 27 additions and 8 deletions

View File

@ -25,6 +25,8 @@ export interface AliPlayerProps {
onEnded?: () => void; onEnded?: () => void;
/** 播放进度更新事件 */ /** 播放进度更新事件 */
onTimeupdate?: (currentTime: number) => void; onTimeupdate?: (currentTime: number) => void;
/** 传递给 AliPlayer 的属性 */
aliPlayerProps?: Record<string, any>;
} }
export interface AliPlayerRef { export interface AliPlayerRef {

View File

@ -19,6 +19,7 @@ const AliPlayer = forwardRef(({
playTime = 0, playTime = 0,
onEnded, onEnded,
onTimeupdate, onTimeupdate,
aliPlayerProps,
}, ref) => { }, ref) => {
const playerRef = useRef(null); const playerRef = useRef(null);
const containerRef = useRef(null); const containerRef = useRef(null);
@ -32,6 +33,7 @@ const AliPlayer = forwardRef(({
pause: () => { pause: () => {
playerRef.current && playerRef.current.pause(); playerRef.current && playerRef.current.pause();
}, },
getPlayerInstance: () => playerRef.current,
})); }));
const onDisposeAliPlayer = () => { const onDisposeAliPlayer = () => {
@ -120,6 +122,7 @@ const AliPlayer = forwardRef(({
isLive, isLive,
useH5Prism: true, useH5Prism: true,
skinLayout, skinLayout,
...aliPlayerProps,
}, },
(player) => { (player) => {
if (autoplay) { if (autoplay) {

View File

@ -1,7 +1,7 @@
import type { ForwardRefExoticComponent, RefAttributes } from "react"; import type { ForwardRefExoticComponent, RefAttributes } from "react";
import type { AliPlayerProps, AliPlayerRef } from "./AliPlayer"; import type { AliPlayerProps, AliPlayerRef } from "./AliPlayer";
export interface VideoProps extends Omit<AliPlayerProps, "onEnded" | "onTimeupdate"> { export interface VideoProps extends AliPlayerProps {
/** 弹窗标题,默认 “视频” */ /** 弹窗标题,默认 “视频” */
title?: string; title?: string;
/** 是否显示弹窗 */ /** 是否显示弹窗 */
@ -15,8 +15,6 @@ export interface VideoProps extends Omit<AliPlayerProps, "onEnded" | "onTimeupda
/** /**
* *
*/ */
declare const Video: ForwardRefExoticComponent< declare const Video: ForwardRefExoticComponent<VideoProps & RefAttributes<AliPlayerRef>>;
VideoProps & RefAttributes<AliPlayerRef>
>;
export default Video; export default Video;

View File

@ -1,12 +1,12 @@
import { Button, Modal } from "antd"; import { Button, Modal } from "antd";
import { useEffect, useRef, useState } from "react"; import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
import { getFileUrl } from "../../utils"; import { getFileUrl } from "../../utils";
import AliPlayer from "./AliPlayer"; import AliPlayer from "./AliPlayer";
/** /**
* 视频播放组件 * 视频播放组件
*/ */
const Video = ({ const Video = forwardRef(({
source = "", source = "",
vid = "", vid = "",
playAuth = "", playAuth = "",
@ -21,10 +21,23 @@ const Video = ({
title = "视频", title = "视频",
visible: externalVisible = false, visible: externalVisible = false,
onCancel, onCancel,
}) => { onEnded,
onTimeupdate,
aliPlayerProps = {},
}, ref) => {
const [internalVisible, setInternalVisible] = useState(false); const [internalVisible, setInternalVisible] = useState(false);
const playerRef = useRef(null); const playerRef = useRef(null);
useImperativeHandle(ref, () => ({
play: () => {
playerRef.current && playerRef.current.play();
},
pause: () => {
playerRef.current && playerRef.current.pause();
},
getPlayerInstance: () => playerRef.current && playerRef.current.getPlayerInstance(),
}));
const visible = onCancel ? externalVisible : internalVisible; const visible = onCancel ? externalVisible : internalVisible;
const setVisible = onCancel || setInternalVisible; const setVisible = onCancel || setInternalVisible;
@ -60,6 +73,9 @@ const Video = ({
isLive={isLive} isLive={isLive}
width={width} width={width}
height={height} height={height}
aliPlayerProps={aliPlayerProps}
onEnded={onEnded}
onTimeupdate={onTimeupdate}
/> />
); );
@ -83,7 +99,7 @@ const Video = ({
} }
return playerElement; return playerElement;
}; });
Video.displayName = "Video"; Video.displayName = "Video";