C# 播放音乐文件代码
5个回答
展开全部
1、调用系统播放类,不过只能播放wav格式的,很占磁盘的。
2、拖Windows Media Player控件
3、可是试试这个API类,不过需要指定文件路径,可以播放MP3 格式的文件
其中filename是文件的绝对路径,包涵扩展名
其它的注释的还算清楚
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO ;
namespace ToBeMyLittlePanda
{
/// <summary>
/// clsMci 的摘要说明。
/// </summary>
public class Mp3Player
{
public Mp3Player()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
private string Name = "" ;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
private string durLength = "" ;
[MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
private string TemStr ="";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
};
public structMCI mc =new structMCI() ;
//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
TemStr ="";
TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
Name = Name.PadLeft(260,Convert.ToChar(" ")) ;
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName,Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length , 0);
ilong = APIClass.mciSendString( Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length , 0);
mc.state = State.mStop;
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length , 0);
mc.state = State.mPlaying ;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop ;
}
public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase ;
}
private string GetCurrPath(string name)
{
if(name.Length <1) return "";
name = name.Trim();
name = name.Substring(0,name.Length-1);
return name;
}
//总时间
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if(durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}
//当前时间
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
}
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName (
string lpszLongPath,
string shortFile,
int cchBuffer
);
[DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString (
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}
2、拖Windows Media Player控件
3、可是试试这个API类,不过需要指定文件路径,可以播放MP3 格式的文件
其中filename是文件的绝对路径,包涵扩展名
其它的注释的还算清楚
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO ;
namespace ToBeMyLittlePanda
{
/// <summary>
/// clsMci 的摘要说明。
/// </summary>
public class Mp3Player
{
public Mp3Player()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
private string Name = "" ;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
private string durLength = "" ;
[MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
private string TemStr ="";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
};
public structMCI mc =new structMCI() ;
//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
TemStr ="";
TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
Name = Name.PadLeft(260,Convert.ToChar(" ")) ;
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName,Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length , 0);
ilong = APIClass.mciSendString( Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length , 0);
mc.state = State.mStop;
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length , 0);
mc.state = State.mPlaying ;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop ;
}
public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase ;
}
private string GetCurrPath(string name)
{
if(name.Length <1) return "";
name = name.Trim();
name = name.Substring(0,name.Length-1);
return name;
}
//总时间
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if(durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}
//当前时间
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
}
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName (
string lpszLongPath,
string shortFile,
int cchBuffer
);
[DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString (
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}
追问
filename在哪啊,没找到啊。还有怎么调用啊,能不能举个例子啊,先谢啦
展开全部
您好,
根据您的需求,您可以调用System.Media.SoundPlayer类播放声音文件,代码如下:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the wav file name:");
string filename = Console.ReadLine();
if (System.IO.File.Exists(filename))
{
SoundPlayer s = new SoundPlayer(filename);
s.Play();
}
Console.WriteLine("Done");
}
}
但是SoundPlayer只支持.wav格式的文件,如果需要播放mp3之类的文件,您也可以调用相应的类库。
根据您的需求,您可以调用System.Media.SoundPlayer类播放声音文件,代码如下:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the wav file name:");
string filename = Console.ReadLine();
if (System.IO.File.Exists(filename))
{
SoundPlayer s = new SoundPlayer(filename);
s.Play();
}
Console.WriteLine("Done");
}
}
但是SoundPlayer只支持.wav格式的文件,如果需要播放mp3之类的文件,您也可以调用相应的类库。
追问
也没有别的类库啊,而且.wav文件也播不了啊,总说指定位置没有可播放的啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-06-26
展开全部
根据您的需求,您可以调用System.Media.SoundPlayer类播放声音文件,代码如下:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the wav file name:");
string filename = Console.ReadLine();
if (System.IO.File.Exists(filename))
{
SoundPlayer s = new SoundPlayer(filename);
s.Play();
}
Console.WriteLine("Done");
}
}
但是SoundPlayer只支持.wav格式的文件,如果需要播放mp3之类的文件,您也可以调用相应的类库。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input the wav file name:");
string filename = Console.ReadLine();
if (System.IO.File.Exists(filename))
{
SoundPlayer s = new SoundPlayer(filename);
s.Play();
}
Console.WriteLine("Done");
}
}
但是SoundPlayer只支持.wav格式的文件,如果需要播放mp3之类的文件,您也可以调用相应的类库。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先要拖控件... 系统自带的Windows Media Player
然后... 把那个那个窗体show出来就行啦...
不过... 你选不同的歌... 会有很多窗体...
你还要用单例模式弄... 这是个大概..
不懂在问我吧
然后... 把那个那个窗体show出来就行啦...
不过... 你选不同的歌... 会有很多窗体...
你还要用单例模式弄... 这是个大概..
不懂在问我吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |