如何在Delphi 10.4中实现媒体播放器组件自动播放及窗体自动切换功能?
我刚好在Delphi 10.4里实现过类似的开场视频自动播放需求,给你一套详细的可落地方案,分步骤来:
实现步骤与代码示例
1. 组件准备与窗体设置
先新建两个窗体:一个是用于播放开场视频的启动窗体(比如命名为frmSplashVideo),另一个是程序的主窗体(frmMain)。在视频窗体上添加以下组件:
TMediaPlayer:负责视频播放控制TMediaPlayerControl:用于显示视频画面,记得把它的MediaPlayer属性关联到上面的TMediaPlayer组件TTimer:用于定时关闭窗体(如果需要固定时长后关闭,而非等视频播放完毕)
然后调整窗体和组件的属性:
- 把
frmSplashVideo的BorderStyle设为bsNone,去掉标题栏;WindowState设为wsMaximized,让视频铺满整个屏幕 TMediaPlayer的AutoOpen和AutoPlay都设为True,确保程序启动后自动加载并播放视频TTimer的Interval设为你想要的自动关闭时长(比如5000表示5秒),默认Enabled设为False,避免提前触发
2. 核心代码实现
视频窗体的事件处理
在frmSplashVideo的单元里,编写以下关键代码:
unit SplashVideoForm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.MPlayer, Vcl.ExtCtrls, Vcl.MPlayerControl; type TfrmSplashVideo = class(TForm) MediaPlayer1: TMediaPlayer; MediaPlayerControl1: TMediaPlayerControl; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure MediaPlayer1Notify(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; var frmSplashVideo: TfrmSplashVideo; implementation uses MainForm; // 引入主窗体的单元 {$R *.dfm} procedure TfrmSplashVideo.FormCreate(Sender: TObject); begin // 设置视频路径:这里假设视频和程序exe在同一目录,文件名为splash.mp4 MediaPlayer1.FileName := ExtractFilePath(Application.ExeName) + 'splash.mp4'; // 启用播放结束通知,用于视频播放完自动关闭的场景 MediaPlayer1.Notify := True; // 如果你想固定时长后关闭(比如5秒),就启用Timer;如果想等视频播完再关,就注释掉这行 Timer1.Enabled := True; end; // 定时关闭的触发事件 procedure TfrmSplashVideo.Timer1Timer(Sender: TObject); begin Timer1.Enabled := False; MediaPlayer1.Stop; // 先停止视频播放 Close; // 关闭当前视频窗体 end; // 视频播放结束的通知事件 procedure TfrmSplashVideo.MediaPlayer1Notify(Sender: TObject); begin if MediaPlayer1.Mode = mpStopped then begin Timer1.Enabled := False; // 关闭Timer(如果之前启用过) Close; end; end; // 窗体关闭时触发,显示主窗体 procedure TfrmSplashVideo.FormClose(Sender: TObject; var Action: TCloseAction); begin // 创建并显示主窗体(如果项目里没自动创建的话) if not Assigned(frmMain) then Application.CreateForm(TfrmMain, frmMain); frmMain.Show; // 释放当前视频窗体 Action := caFree; end; end.
调整项目启动配置
打开项目的.dpr文件,修改启动逻辑,让程序先加载视频窗体,而非主窗体:
program MyVideoSplashApp; uses Vcl.Forms, SplashVideoForm in 'SplashVideoForm.pas' {frmSplashVideo}, MainForm in 'MainForm.pas' {frmMain}; {$R *.res} begin Application.Initialize; Application.MainFormOnTaskbar := True; // 只创建视频窗体,主窗体在视频窗体关闭时再创建 Application.CreateForm(TfrmSplashVideo, frmSplashVideo); // 注释掉自动创建主窗体的代码 // Application.CreateForm(TfrmMain, frmMain); Application.Run; end;
3. 进阶优化:嵌入视频资源
如果不想把视频文件单独放在exe目录下,可以把视频嵌入到程序资源里,运行时提取到临时目录播放:
procedure TfrmSplashVideo.FormCreate(Sender: TObject); var ResStream: TResourceStream; TempVideoPath: string; begin // 生成临时文件路径 TempVideoPath := TPath.Combine(TPath.GetTempPath, 'splash_temp.mp4'); // 从资源中读取视频并保存到临时文件 ResStream := TResourceStream.Create(HInstance, 'SPLASH_VIDEO', 'VIDEO'); try ResStream.SaveToFile(TempVideoPath); MediaPlayer1.FileName := TempVideoPath; finally ResStream.Free; end; MediaPlayer1.AutoOpen := True; MediaPlayer1.AutoPlay := True; Timer1.Enabled := True; end; procedure TfrmSplashVideo.FormClose(Sender: TObject; var Action: TCloseAction); begin // 删除临时视频文件 if FileExists(MediaPlayer1.FileName) then DeleteFile(MediaPlayer1.FileName); if not Assigned(frmMain) then Application.CreateForm(TfrmMain, frmMain); frmMain.Show; Action := caFree; end;
要嵌入资源,需要在项目里添加.rc资源脚本文件,内容如下:
SPLASH_VIDEO VIDEO "splash.mp4"
然后把这个.rc文件添加到项目中,编译后视频就会嵌入到exe里。
关键注意事项
- 视频格式兼容性:VCL的
TMediaPlayer依赖Windows的DirectShow框架,确保你的视频格式(如MP4、AVI)是系统支持的,若播放失败可以安装通用解码器(比如K-Lite Codec Pack) - 路径处理:用
ExtractFilePath(Application.ExeName)获取程序所在目录,避免硬编码路径导致找不到视频文件 - 窗体体验:可以给视频窗体添加
OnKeyDown事件,允许用户按ESC键提前关闭视频,提升交互性
内容的提问来源于stack exchange,提问作者Franklin
相关产品推荐
相关产品推荐

