学习了参考文献1中的示例代码,也找出了前一篇文章中自己测试控件但无法播放视频的问题(没有将解码库文件复制到可执行程序所在的codecs文件夹内),本文基于APlayer组件编写简单的视频播放器,主要实现以下功能:
序号 | 功能 | 说明 |
---|---|---|
1 | 自定义logo | 调用SetCustomLogo函数设置未播放视频时的显示图片,图片来自参考文献2 |
2 | 打开文件 | 调用Open函数打开并播放视频文件 |
3 | 定时刷新播放信息 | 参照参考文献1中的demo,使用定时器定时刷新当前播放进度,调用GetDuration获取视频总时长,调用GetPosition获取当前播放时间,同时在OnOpenSucceeded中启动定时器,在OnStateChanged事件中停止定时器 |
4 | 暂停/播放/停止 | 调用Pause/Play/Close函数暂停/播放/停止视频 |
5 | 设置音量 | 调用SetVolume设置音量,尽在0-100范围内调节音量,并未使用APlayer的10倍放大 |
6 | 拖拽时间条 | 调用SetPosition函数在Trackbar的Scroll事件中重新设置播放时间 |
7 | 快进/快退 | 调用SetPosition函数快进/快退10秒 |
最后是程序界面、播放视频截图及主要代码:
文章来源:https://www.toymoban.com/news/detail-744256.html
public Form1()
{
this.axPlayer1.SetCustomLogo(((Bitmap)Bitmap.FromFile("gundam.jpg")).GetHbitmap().ToInt32());
axPlayer1.OnOpenSucceeded += new EventHandler(OnOpenSucceeded);
axPlayer1.OnStateChanged += new _IPlayerEvents_OnStateChangedEventHandler(OnStateChanged);
}
string TimeToString(TimeSpan span)
{
return span.Hours.ToString("00") + ":" +
span.Minutes.ToString("00") + ":" +
span.Seconds.ToString("00");
}
void OnOpenSucceeded(object sender, EventArgs e)
{
lblProgress.Text= "00:00:00/"+TimeToString(TimeSpan.FromMilliseconds(axPlayer1.GetDuration()));
tbTimeline.Maximum = axPlayer1.GetDuration();
timer1.Start();
}
private void UpdatePlayInfo(object sender, EventArgs e)
{
lblProgress.Text = TimeToString(TimeSpan.FromMilliseconds(axPlayer1.GetPosition()))+"/" + TimeToString(TimeSpan.FromMilliseconds(axPlayer1.GetDuration()));
tbTimeline.Value = axPlayer1.GetPosition() < 0 ? 0 : axPlayer1.GetPosition();
}
private void tbVolumn_ValueChanged(object sender, EventArgs e)
{
axPlayer1.SetVolume(tbVolumn.Value);
}
private void btnForward_Click(object sender, EventArgs e)
{
axPlayer1.SetPosition(axPlayer1.GetPosition() + 10000);
}
private void btnBack_Click(object sender, EventArgs e)
{
int timeLine = axPlayer1.GetPosition() - 10000;
axPlayer1.SetPosition(timeLine < 0 ? 0 : timeLine);
}
private void btnClose_Click(object sender, EventArgs e)
{
axPlayer1.Close();
}
private void btnPlay_Click(object sender, EventArgs e)
{
axPlayer1.Play();
}
private void btnPause_Click(object sender, EventArgs e)
{
axPlayer1.Pause();
}
void OnStateChanged(object sender, _IPlayerEvents_OnStateChangedEvent e)
{
switch (e.nNewState)
{
case 0:
tbTimeline.Value = 0;
lblProgress.Text = "00:00:00/00:00:00" ;
timer1.Stop();
break;
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "mp4|*.mp4|avi|*.avi|rm|*.rm|rmvb|*.rmvb|flv|*.flv|xr|*.xr|所有文件|*.*";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
axPlayer1.Open(ofd.FileName);
}
}
}
private void tbTimeline_Scroll(object sender, EventArgs e)
{
axPlayer1.SetPosition(tbTimeline.Value);
lblProgress.Text = TimeToString(TimeSpan.FromMilliseconds(axPlayer1.GetPosition())) + "/" + TimeToString(TimeSpan.FromMilliseconds(axPlayer1.GetDuration()));
}
参考文献:
[1]https://blog.csdn.net/qq_45533937/article/details/115800927
[2]https://www.hpoi.net/hobby/10836文章来源地址https://www.toymoban.com/news/detail-744256.html
到了这里,关于测试C#调用Aplayer播放视频(3:编写简易播放器)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!