网上也有许多C#播放音乐的方法,主要就是WindowsAPI以及MediaPlayer还有自带的那个Media命名空间的SoundPlayer类引用。
用MediaPlayer引用的话 一般用于播放WMA之类的音频文件,MP3的话 需要W7系统才能播放,SoundPlayer只能播放WAV格式音乐。
如果想播放MP3之类的音频就要使用WindowsAPI调用,调用方法如下:
using System.Runtime.InteropServices;
public static uint SND_ASYNC = 0x0001;
public static uint SND_FILENAME = 0x00020000;
[DllImport("winmm.dll")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);
public void Play()
{
mciSendString(@"close temp_alias", null, 0, 0);
mciSendString(@"open ""这里写音乐地址"" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
调用API方法对于新手来说或许过于复杂,我这里写了一个DLL也是专门用于播放音乐的,欢迎大家下载使用!
使用方法:
导入引用 – LolitaSound.dll – 代码顶部导入命名空间 – using LolitaSound;
实例化方法:
LSound xx = new LSound("URL"); //URL为音频文件地址
方法:
SetSoundURL("URL") //定义新的地址
GetSoundURL() //获取当前实例地址
Play() //播放音乐
Stop() //停止播放
下载地址
很详细的说明书。
我顶