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;
}
}
}

转载于:https://www.cnblogs.com/zwj-199306231519/p/10614693.html

1、基本属性  URL 指定媒体位置,本机或网络地址 例如:axWindowsMediaPlayer1.URL = @"f:\aa.mp3"; uiMode 播放器界面模式,可为Full, Mini, None, Invisible ;Full:有下面的控制条; None:只有播放部份没有控制条 例如:axWindowsMediaPlayer1.uiMode = "Invisible"; playState 播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪 enableContextMenu 启用/禁用右键菜单 fullScreen 是否全屏显示 stretchToFit 非全屏状态时是否伸展到最佳大小 2、播放器基本控制 Ctlcontrols.play(); 播放 Ctlcontrols.pause(); 暂停 Ctlcontrols.stop(); 停止 Ctlcontrols.currentPosition 当前进度 Ctlcontrols.currentPositionString 当前进度,字符串格式。如“00:23” Ctlcontrols.fastForward(); 快进 Ctlcontrols.fastReverse(); 快退 Ctlcontrols.next(); 下一曲 Ctlcontrols.previous(); 上一曲 3、播放器基本设置 settings.volume 音量,0-100 settings.balance 声道,通过它应该可以进行立体声、左声道、右声道的控制。 settings.autoStart 是否自动播放 settings.mute 是否静音 settings.playCount 播放次数 settings.rate 播放速度 4、当前媒体属性 currentMedia.duration 媒体总长度 currentMedia.durationString 媒体总长度,字符串格式。如“03:24” currentMedia.getItemInfo(const string) 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址 currentMedia.setItemInfo(const string) 通过属性名设置媒体信息 currentMedia.name 同 currentMedia.getItemInfo("Title")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值