新手求教MP3播放器问题

yjtxpj 2010-09-13 03:39:39
最近做了一个简单的播放器,使用的是DirectSound,但发现只能播放wav格式的音频文件。怎么样才能播放MP3呢(不使用media player等方法)谢谢各位了
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
CHN_smallfox 2010-09-13
  • 打赏
  • 举报
回复

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);
}
public class Mp3Helper
{
//播放
public static void Play(string strFileName)
{
string buf = "";
buf = buf.PadLeft(128, ' ');
strFileName = "open " + Convert.ToChar(34) + strFileName + Convert.ToChar(34) + " alias media";
APIClass.mciSendString(strFileName, buf, buf.Length, 0); //初始化音频文件
APIClass.mciSendString("play media", buf, buf.Length, 0); //播放音频文件

}

//关闭
public static void Stop()
{
APIClass.mciSendString("close media", "", 0, 0);
}
//暂停
public static void Pause()
{
APIClass.mciSendString("pause media", "", 0, 0);
}
}
public class Information
{
//总时间
public static int GetMp3Lenth()
{
string durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();

if (string.IsNullOrEmpty(durLength))

return 0;

else

return Convert.ToInt32(durLength) / 1000;

}
//当前播放位置
public static int CurrentPosition()
{
string buf = "";
buf = buf.PadLeft(128, ' ');
APIClass.mciSendString("status media position", buf, buf.Length, 0);
buf = buf.Trim();
if (string.IsNullOrEmpty(buf))
return 0;
else
return (int)(Convert.ToDouble(buf)) / 1000;
}


//进度控制
public static bool SetProcess(int process)
{
bool result = false;
string MciCommand = string.Format("seek media to {0}", process);
int RefInt = APIClass.mciSendString(MciCommand, process.ToString(), 0, 0);
APIClass.mciSendString("play media", null, 0, 0);
if (RefInt == 0)
{
result = true;
}

return result;
}
//声音控制
public static bool SetValume(int Valume)
{
bool result = false;
string MciCommand = string.Format("setaudio media volume to {0}", Valume);
int RefInt = APIClass.mciSendString(MciCommand, null, 0, 0);
if (RefInt == 0)
{
result = true;
}

return result;
}

CHN_smallfox 2010-09-13
  • 打赏
  • 举报
回复
可以调用一些API
周药师 2010-09-13
  • 打赏
  • 举报
回复
用DirectShow不仅可以播放MP3还可以播放视频
周药师 2010-09-13
  • 打赏
  • 举报
回复
用DirectShow可以播放的
http://download.csdn.net/source/1010585
llf94632525 2010-09-13
  • 打赏
  • 举报
回复
解码器?

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧