C#使用WindowsMediaPlayer实现视频播放

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WindowsFormsApplication2
{


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string playPath = null;
string directory = null;
List<string> playArray = null;
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "mp4|*.mp4|视频文件(*.avi;*.wmv)|(*.avi;*.wmv)|All File(*.*)|(*.*)";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
playPath = openFileDialog.FileName;
directory = Path.GetDirectoryName(playPath);

playArray = Form1.GetplayCollection(directory);

}
axWindowsMediaPlayer1.URL = playPath;

}
}

public static List<string> GetplayCollection(string path)
{
string[] playarray = Directory.GetFiles(path);
var result = from playstring in playarray
where playstring.EndsWith("wmv", StringComparison.OrdinalIgnoreCase) ||
playstring.EndsWith("avi", StringComparison.OrdinalIgnoreCase)||
playstring.EndsWith("mp4", StringComparison.OrdinalIgnoreCase)
select playstring;
return result.ToList();
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void 播放暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((int)axWindowsMediaPlayer1.playState == 2)
axWindowsMediaPlayer1.Ctlcontrols.play();
else if ((int)axWindowsMediaPlayer1.playState == 3)
axWindowsMediaPlayer1.Ctlcontrols.pause();
}

private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}

private void 快进ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.fastForward();
}

private void 快退ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.fastReverse();
}

private void 上一曲ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.previous();
}

private void 下一曲ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.next();
}

private void 升高音量ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (axWindowsMediaPlayer1.settings.volume <= 90)
{
axWindowsMediaPlayer1.settings.volume += 10;
}
else
{
axWindowsMediaPlayer1.settings.volume = 100;
}
}

private void 降低音量ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (axWindowsMediaPlayer1.settings.volume >= 10)
{
axWindowsMediaPlayer1.settings.volume -= 10;
}
else
{
axWindowsMediaPlayer1.settings.volume = 0;
}
}

private void 静音ToolStripMenuItem_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = 0;
}
}
}

posted @ 2019-03-28 14:21  zwj鹿港小镇  阅读(2705)  评论(0编辑  收藏  举报