您的位置:首页 > 编程语言 > C#

C#播放器控件的常用方法介绍

2015-05-15 20:28 183 查看
右击工具箱->选择项(I)... -> 显示"选择工具箱项" -> COM组件 -> Windows Media Player wmp.dll 添加

[基本属性]  
URL:String; 指定媒体位置,本机或网络地址
uiMode:String; 播放器界面模式,可为Full, Mini, None, Invisible(不计大小写)
playState:integer; 播放状态。这个属性改变时同时引发PlayStateChange事件与StateChange事件。取值范围为枚举型:WMPLib.WMPPlayState,它的成员如下:
  wmppsUndefined = 0;   //未知状态
  wmppsStopped = 1;    //播放停止
  wmppsPaused = 2;     //播放暂停
  wmppsPlaying = 3;     //正在播放
  wmppsScanForward = 4;   //向前搜索
  wmppsScanReverse = 5;   //向后搜索
  wmppsBuffering = 6;     //正在缓冲
  wmppsWaiting = 7;      //正在等待流开始
  wmppsMediaEnded = 8;    //播放流已结束
  wmppsTransitioning = 9;    //准备新的媒体文件
  wmppsReady = 10;      //播放准备就绪
  wmppsReconnecting = 11;   //尝试重新连接流媒体数据
  wmppsLast = 12;       //上一次状态,状态没有改变
  在PlayStateChange中写代码可以防止播放rmvb等非默认类型的问题(用wmppsReady)。
  enableContextMenu:Boolean;    启用/禁用右键菜单
  fullScreen:boolean;         是否全屏显示

  //播放器基本控制
  Ctlcontrols.play; 播放
  Ctlcontrols.pause; 暂停
  Ctlcontrols.stop; 停止
  Ctlcontrols.currentPosition:double; 当前进度
  Ctlcontrols.currentPositionString:string; 当前进度,字符串格式。如“00:23”
  Ctlcontrols.fastForward; 快进
  Ctlcontrols.fastReverse; 快退
  Ctlcontrols.next; 下一曲
  Ctlcontrols.previous; 上一曲

  [settings] wmp.settings //播放器基本设置
  settings.volume:integer; 音量,0-100
  settings.autoStart:Boolean; 是否自动播放
  settings.mute:Boolean; 是否静音
  settings.playCount:integer; 播放次数
  //顺序播放
  wmp.settings.setMode("shuffle", False)
  //随机播放
  wmp.settings.setMode("shuffle", True)
  //循环播放
  wmp.settings.setMode("loop", True)

[currentMedia] wmp.currentMedia //当前媒体属性
currentMedia.duration:double; 媒体总长度
currentMedia.durationString:string; 媒体总长度,字符串格式。如“03:24”
currentMedia.getItemInfo(const string); 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述, "Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址
currentMedia.setItemInfo(const string); 通过属性名设置媒体信息
currentMedia.name:string; 同 currentMedia.getItemInfo("Title")


基本设置实例:

axWindowsMediaPlayer1.windowlessVideo = false;   //设为false后双击屏幕可以全屏
axWindowsMediaPlayer1.fullScreen = true; //设播放器全屏播放

axWindowsMediaPlayer1.URL = @"mms://192.168.0.102/vod/jingwei.wma";//播放资源

axWindowsMediaPlayer1.Ctlcontrols.play();        //播放
axWindowsMediaPlayer1.Ctlcontrols.stop();        //停止
axWindowsMediaPlayer1.Ctlcontrols.pause();       //暂停

axWindowsMediaPlayer1.settings.autoStart = true;     //自动播放

axWindowsMediaPlayer1.settings.mute = false;         //静音
axWindowsMediaPlayer1.settings.volume = 100;      // 音量 int 0 ~ 100   100 是最大音量

axWindowsMediaPlayer1.currentMedia.duration.ToString();//影片长度
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 30; //当前的播放位置 double

axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");//标题
axWindowsMediaPlayer1.currentMedia.getItemInfo("Author");//作者


全屏控制实例代码:

using System.IO;
using WMPLib;

public videoPlay()
{
InitializeComponent();

//全屏设置及隐藏鼠标
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
//Cursor.Hide();
//播放器全屏
Rectangle screenSize = System.Windows.Forms.SystemInformation.VirtualScreen;//获取屏幕的宽和高
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Size = new System.Drawing.Size(screenSize.Width,screenSize.Height);
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(screenSize.Width, screenSize.Height);
//播放器设置
axWindowsMediaPlayer1.uiMode = "None";//播放器样式
axWindowsMediaPlayer1.stretchToFit = true;//非全屏状态时是否伸展到最佳大小
axWindowsMediaPlayer1.enableContextMenu = false;//禁用播放器右键菜单

}

private IWMPPlaylist videoList;//创建播放列表
private bool ifLoop = true;//视频是否循环

//设置是否循环播放
public bool IfLoop
{
get { return ifLoop; }
set { ifLoop = value; }
}

//播放状态改变时发生
private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
{
//判断视频是否已停止播放
if ((int)axWindowsMediaPlayer1.playState == 1)
{
//停顿2秒钟再重新播放
//System.Threading.Thread.Sleep(2000);
//重新播放
//axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
//播放
public void videoStart()
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
//列表播放
public void videoListStart()
{
videoPlayList();//重新获取播放列表
axWindowsMediaPlayer1.Ctlcontrols.play();
}
//暂停
public void videoPause()
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
//重播
public void videoReplay()
{
videoStop();
videoStart();
}
//列表重播
public void videoListReplay()
{
axWindowsMediaPlayer1.currentPlaylist = videoList;//重新载入播放列表
videoStart();
}
//停止播放
public void videoStop()
{
//axWindowsMediaPlayer1.currentPlaylist.clear();//清除列表
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
//视频静音
public void videoMute(bool t)
{
axWindowsMediaPlayer1.settings.mute = t;
}
//播放下一个视频
public void videoNext()
{
//判断当前所播放的视频是否是列表的最后一个
if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[axWindowsMediaPlayer1.currentPlaylist.count - 1].name)
{
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.next();//播放下一个
}
}
//播放上一个媒体
public void videoPrevious()
{  //判断当前所播放的视频是否是列表的第一个
if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[0].name)
{
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.previous();//播放上一个
}
}

//获取播放类表及初始化
public void videoPlayList()
{
videoList = axWindowsMediaPlayer1.playlistCollection.newPlaylist("one");//创建播放列表
string path = @".\data\video";//媒体路径
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
FileInfo fi = (FileInfo)fsi;
videoList.appendItem(axWindowsMediaPlayer1.newMedia(fi.FullName));
}
}
axWindowsMediaPlayer1.currentPlaylist = videoList;//查找到视频、播放类表
axWindowsMediaPlayer1.settings.setMode("loop", ifLoop);//设置类表循环播放
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: