我需要嵌入一个可以播放视频文件并使用字幕(.srt)的播放器。
哪些播放器(具有良好文档)可用于此任务?以及如何设置文件和字幕?
添加一个 Windows Media Player COM 对象。
添加计时器
private Timer playingTimer = new Timer();
public Main()
{
InitializeComponent();
playingTimer.Enabled = false;
playingTimer.Tick += renderSubtitles;
}
处理 StatusChange 以了解它何时开始播放
private void Main_Load(object sender, EventArgs e)
{
player.StatusChange += player_StatusChange;
}
播放时激活计时器
void player_StatusChange(object sender, EventArgs e)
{
if (player.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
playingTimer.Enabled = true;
}
else
{
playingTimer.Enabled = false;
}
}
从 获取当前秒数player.Ctlcontrols.currentPosition
,如果找到字幕使标签可见,则设置文本并水平居中。如果没有任何字幕,请隐藏标签。
private void renderSubtitles(object sender, EventArgs e)
{
var ts = TimeSpan.FromSeconds(player.Ctlcontrols.currentPosition);
var entry = entries.FirstOrDefault(o => o.Start <= ts && o.End >= ts);
if (entry != null)
{
subtitle.Visible = true;
subtitle.Text = entry.Text;
var w = splitContainer.Panel1.Width;
subtitle.Location = new Point(w / 2 - subtitle.Width / 2, subtitle.Location.Y);
}
else
{
subtitle.Visible = false;
}
}