C# 播放音乐有很多方法,比如API的PlaySound,微软的TTS(Text To Speech)也可以,但是这两个没法控制音量(TTS在读Text的时候可以控制音量大小,但是在播放wav等文件时控制音量就无效),还有DirectSound可以调节音量(需要引用Microsoft.DirectX.dll和Microsoft.DirectX.DirectSound.dll,有需要的朋友可点这里>>>下载),但是用这个在64位机器下会报错,虽然可以改成x86编译后使用,但有时项目必须要用64位编译,没辙!只能另辟蹊径
后面还是回归到API,当然不是PlaySound,而是mciSendString,搜一下mciSendString用法有很多,我这里就只记录基本的几个方法(我所需要的)
public class MCIMediaHelper
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer);
/// <summary>
/// 音量大小
/// </summary>
public static int Volume;
/// <summary>
/// 播放状态
/// </summary>
public enum Playstate : byte
{
Stopped = 1,
Playing = 2,
Pause = 3
}
/// <summary>
/// 打开MCI设备
/// </summary>
/// <param name="FileName">要打开的文件名</param>
/// <returns>传值代表成功与否</returns>
private static bool OpenMusic(string FileName)
{
CloseMusic();
string ShortPathName = "";
ShortPathName = ShortPathName.PadLeft(260, Convert.ToChar(" "));
int RefInt = GetShortPathName(FileName, ShortPathName, ShortPathName.Length);
ShortPathName = GetCurrPath(ShortPathName);
//不知道播放wav文件用waveaudio为什么不能单曲循环播放?
string MciCommand = string.Format("open {0} type MPEGVideo alias NOWMUSIC ", ShortPathName);//"open " & RefShortName & " type " & DriverID & " alias NOWMUSIC"
RefInt = mciSendString(MciCommand, null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 循环播放音乐
/// </summary>
/// <returns></returns>
public static bool PlayMusic(string musicPath)
{
if (OpenMusic(musicPath))
{
int RefInt = mciSendString("play NOWMUSIC repeat", null, 0, 0);
if (RefInt == 0)
{
SetVolume();
}
return RefInt == 0;
}
return false;
}
/// <summary>
/// 暂停播放
/// </summary>
/// <returns></returns>
public static bool PauseMusic()
{
int RefInt = mciSendString("pause NOWMUSIC", null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 继续播放
/// </summary>
/// <returns></returns>
public static bool ResumeMusic()
{
int RefInt = mciSendString("resume NOWMUSIC", null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 停止播放
/// </summary>
/// <returns></returns>
public static bool StopMusic()
{
int RefInt = mciSendString("stop NOWMUSIC", null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 设置声音大小(1-1000)
/// </summary>
/// <returns></returns>
private static bool SetVolume()
{
string MciCommand = string.Format("setaudio NOWMUSIC volume to {0}", Volume);
int RefInt = mciSendString(MciCommand, null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 设置播放速度
/// </summary>
/// <param name="Speed"></param>
/// <returns></returns>
private static bool SetSpeed(int Speed)
{
string MciCommand = string.Format("set NOWMUSIC speed to {0}", Speed);
int RefInt = mciSendString(MciCommand, null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 关闭媒体
/// </summary>
/// <returns></returns>
public static bool CloseMusic()
{
int RefInt = mciSendString("close NOWMUSIC", null, 0, 0);
return RefInt == 0;
}
/// <summary>
/// 获取播放状态
/// </summary>
/// <returns></returns>
public static Playstate IsPlaying()
{
Playstate isPlaying = Playstate.Stopped;
string durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
int RefInt = mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放")
{
isPlaying = Playstate.Playing;
}
else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止")
{
isPlaying = Playstate.Stopped;
}
else
{
isPlaying = Playstate.Pause;
}
return isPlaying;
}
private static string GetCurrPath(string name)
{
if (name.Length < 1)
{
return "";
}
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}