都要添加引用
c 盘下的 WMPLib
asp.net 代码
<object ID="nsPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" class="unnamed2" style="width: 608px; height: 534px" >
<param name="url" value="<%= path %>" >
<param name="PlayCount" value="<%= a %>">
<param name="MovieWindowHeight" value="540">
<param name="MovieWindowWidth" value="600">
</object>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WMPLib;
using System.IO;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
public int a = 1;//播放次数
public static int cc = 0;
string patha = "";
public string path = "";
public static string filepath ="";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
patha = Request.ApplicationPath;//得到虚拟目录
filepath = Server.MapPath(patha)+"//music";
DirectoryInfo dinfo = new DirectoryInfo(filepath);
foreach (FileInfo fio in dinfo.GetFiles("*.wmv"))
{
this.LstOne.Items.Add(fio.Name);
}
this.LstOne.DataBind();
if (this.LstOne.Items.Count > 0)
{
path = Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf('/')) + "//music//" + this.LstOne.Items[0].Value.ToString();
}
}
}
protected void LstOne_SelectedIndexChanged(object sender, EventArgs e)
{
string url = Request.Url.ToString();
string pp = url.LastIndexOf('/').ToString();
path = Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf('/')) + "//music//" + this.LstOne.SelectedItem.ToString();
}
}
winform 代码
private void Form1_Load(object sender, EventArgs e)
{
WMPLib.WindowsMediaPlayer w = new WindowsMediaPlayer();
w.currentPlaylist.appendItem(w.newMedia("D://tet.wma"));
w.currentPlaylist.appendItem(w.newMedia("D://b.wma"));
w.URL = "D://tet.wma";
w.controls.play();
}
private void button1_Click(object sender, EventArgs e)
{
string filepath = @"D:/player/";
DirectoryInfo dinfo = new DirectoryInfo(filepath);
foreach (FileInfo fio in dinfo.GetFiles())
{
axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(filepath + fio.Name));
}
this.axWindowsMediaPlayer1.Ctlcontrols.play();
}