@remotion/player 的示例
裸例
🌐 Bare example
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
/>
);
};添加控件
🌐 Adding controls
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
controls
/>
);
};循环视频
🌐 Loop video
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
controls
loop
/>
);
};改变大小
🌐 Changing size
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
controls
loop
style ={{
width : 1280,
height : 720,
}}
/>
);
};note
有关更多放大播放器的方法,请参见 Scaling。
添加自动播放
🌐 Adding autoplay
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
controls
loop
style ={{
width : 1280,
height : 720,
}}
autoPlay
/>
);
};tip
你需要警惕浏览器的自动播放策略。在大多数浏览器中,你无法自动播放音频文件或带音频的视频。
以编程方式控制播放器
🌐 Programmatically control the player
import { Player , PlayerRef } from "@remotion/player";
import { useCallback , useRef } from "react";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
const playerRef = useRef <PlayerRef >(null);
const seekToMiddle = useCallback (() => {
const { current } = playerRef ;
if (!current ) {
return;
}
current .seekTo (60);
}, []);
return (
<Player
ref ={playerRef }
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
/>
);
};聆听事件
🌐 Listen to events
import { Player , PlayerRef } from "@remotion/player";
import { useEffect , useRef } from "react";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
const playerRef = useRef <PlayerRef >(null);
useEffect (() => {
const { current } = playerRef ;
if (!current ) {
return;
}
const listener = () => {
console .log ("paused");
};
current .addEventListener ("pause", listener );
return () => {
current .removeEventListener ("pause", listener );
};
}, []);
return (
<Player
ref ={playerRef }
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
/>
);
};互动播放器
🌐 Interactive player
import { Player } from "@remotion/player";
import { useState , useMemo } from "react";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
// Connect the state to a text field
const [text , setText ] = useState ("world");
const inputProps = useMemo (() => {
return {
text ,
};
}, [text ]);
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
inputProps ={inputProps }
/>
);
};只播放视频的一部分
🌐 Only play a portion of a video
import { Player } from "@remotion/player";
import { MyVideo } from "./remotion/MyVideo";
export const App : React .FC = () => {
return (
<Player
component ={MyVideo }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
loop
inFrame ={30}
outFrame ={60}
/>
);
};懒加载组件
🌐 Loading a component lazily
import { Player } from "@remotion/player";
import { useCallback } from "react";
export const App : React .FC = () => {
const lazyComponent = useCallback (() => {
return import("./remotion/MyVideo");
}, []);
return (
<Player
lazyComponent ={lazyComponent }
durationInFrames ={120}
compositionWidth ={1920}
compositionHeight ={1080}
fps ={30}
loop
/>
);
};另请参阅
🌐 See also