1. 程式人生 > 其它 >C#圖片視訊混合輪播(播放時長不同)

C#圖片視訊混合輪播(播放時長不同)

C#實現圖片與視訊混合輪播

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//去掉窗體自帶樣式
this.FormBorderStyle = FormBorderStyle.None;
//視窗最大化
this.WindowState = FormWindowState.Maximized;

//將圖片鋪滿螢幕
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//將視訊鋪滿螢幕
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.uiMode = "None";

}

//節目索引
int index = 0;
//節目時長索引
int count = 0;
//節目集合
List<string> materialFileList = new List<string>();
//節目時長集合
List<int> materialTimeList = new List<int>();
//System.Timers.Timer t = new System.Timers.Timer(5000);
//System.Timers.Timer t2 = new System.Timers.Timer(500);
System.Threading.Timer t1 = null;

//System.Threading.Timer t1 = new System.Threading.Timer();

public static string[] ImgPath()
{
string currentPath = Environment.CurrentDirectory;
string[] imageFile = Directory.GetFiles(currentPath + "\\imgs");
return imageFile;
}
//獲取存放視訊資料夾下所有視訊名稱
public static string[] VideoPath()
{
string currentPath = Environment.CurrentDirectory;
string[] videoFile = Directory.GetFiles(currentPath + "\\videos");
return videoFile;
}

private void Form2_Load(object sender, EventArgs e)
{
pictureBox1.Hide();
axWindowsMediaPlayer1.Hide();
//獲取本地素材的絕對路徑
var sb = ImgPath();
var sbb = VideoPath();
for (int i = 0; i < sb.Length; i++)
{
materialFileList.Add(sb[i]);
}
for (int i = 0; i < sbb.Length; i++)
{
materialFileList.Add(sbb[i]);
}

//模擬後臺系統釋出的單個節目時長
materialTimeList.Add(5);
materialTimeList.Add(10);
materialTimeList.Add(15);
materialTimeList.Add(20);
materialTimeList.Add(11);
materialTimeList.Add(7);
materialTimeList.Add(15);
t1 = new System.Threading.Timer(p => showMaterial(), null,1, materialTimeList[0] * 1000);
}
private void showMaterial()
{
var temp = materialFileList[index].ToString();
var materialFormat = temp.Remove(0, temp.LastIndexOf("."));
if (materialFormat == ".jpg" || materialFormat == ".png")
{
this.Invoke(new Action(() =>
{
pictureBox1.Show();
axWindowsMediaPlayer1.Hide();
pictureBox1.LoadAsync(materialFileList[index]);
index++;
if (index > materialFileList.Count - 1) index = 0;
t1.Change(materialTimeList[count] * 1000,Timeout.Infinite);
count++;
if (count > materialTimeList.Count - 1) count = 0;
}));
}
else if (materialFormat == ".mp4" || materialFormat == ".avi")
{
this.Invoke(new Action(() =>
{
pictureBox1.Hide();
axWindowsMediaPlayer1.Show();
axWindowsMediaPlayer1.URL = materialFileList[index];
index++;
if (index > materialFileList.Count - 1) index = 0;
t1.Change(materialTimeList[count] * 1000, Timeout.Infinite);
count++;
if (count > materialTimeList.Count - 1) count = 0;
}));
}
}
}
}