第二种方法就是利用dirextx sdk开发包中的API来开发,这个开发包里面有个DierctSound可以用来开发,在网上找了好久他的中文开发文档,大致看了一遍,就决定动手做了。
首先是对DirectSound中的类我需要的调用的函数进行了封装,封装成两个结构体,一个枚举,一个静态类,一个播放类,类图如下:
其中DeviceInfo结构体是硬件信息也是就是声卡的硬件信息,DevId表示硬件的ID号,Description是对硬件的描述,这些信息都是通过DirectSound从声卡的驱动程序中读取出来的。
/// <summary>
/// 设备信息
/// </summary>
public struct DeviceInfo
{
public Guid DevId;
public string Description;
}
/// 设备信息
/// </summary>
public struct DeviceInfo
{
public Guid DevId;
public string Description;
}
DeviceStatus结构体记录播放器的状态,isLooping表示是否循环播放,isPlaying表示正处于播放还是停止,isTerminated表示播放终止。
/// <summary>
/// 播放状态
/// </summary>
public struct DeviceStatus
{
/// <summary>
/// 是否播放
/// </summary>
public bool isPlaying;
/// <summary>
///是否循环播放
/// </summary>
public bool isLooping;
/// <summary>
/// 是否终止
/// </summary>
public bool isTerminated;
}
/// 播放状态
/// </summary>
public struct DeviceStatus
{
/// <summary>
/// 是否播放
/// </summary>
public bool isPlaying;
/// <summary>
///是否循环播放
/// </summary>
public bool isLooping;
/// <summary>
/// 是否终止
/// </summary>
public bool isTerminated;
}
PlayFlags枚举指定播放器是正常播放还是循环播放。
/// <summary>
/// 设置播放的方式
/// </summary>
public enum PlayFlags
{
/// <summary>
/// 正常播放
/// </summary>
Default,
/// <summary>
/// 循环播放
/// </summary>
Looping
};
/// 设置播放的方式
/// </summary>
public enum PlayFlags
{
/// <summary>
/// 正常播放
/// </summary>
Default,
/// <summary>
/// 循环播放
/// </summary>
Looping
};
静态类QBDeviceHelper中的方法GetDeviceList()用来获得机器的声卡列表。
public static class QBDeviceHelper
{
/// <summary>
/// 获得设备列表(返回设备信息)
/// </summary>
/// <returns></returns>
public static List<DeviceInfo> GetDeviceList()
{
List<DeviceInfo> devList = new List<DeviceInfo>();
DevicesCollection myDevices = new DevicesCollection();//获得所有声卡列表
foreach (DeviceInformation dev in myDevices)
{
DeviceInfo dData = new DeviceInfo();
dData.DevId = dev.DriverGuid;
dData.Description = dev.Description;
devList.Add(dData);
}
return devList;
}
}
{
/// <summary>
/// 获得设备列表(返回设备信息)
/// </summary>
/// <returns></returns>
public static List<DeviceInfo> GetDeviceList()
{
List<DeviceInfo> devList = new List<DeviceInfo>();
DevicesCollection myDevices = new DevicesCollection();//获得所有声卡列表
foreach (DeviceInformation dev in myDevices)
{
DeviceInfo dData = new DeviceInfo();
dData.DevId = dev.DriverGuid;
dData.Description = dev.Description;
devList.Add(dData);
}
return devList;
}
}
播放器类QBAudioCtrl是用来控制播放器的播放,停止等动作
public class QBAudioCtrl
{
SecondaryBuffer sbuffer = null;
public QBAudioCtrl(Guid DevId, string FileName)
{
try
{
//根据ID建立设备对象
Device dev = new Device(DevId);
//设置设备的优先级
dev.SetCooperativeLevel((new System.Windows.Forms.Form()), CooperativeLevel.Priority);
//设置缓存的属性
BufferDescription bfDes = new BufferDescription();
bfDes.ControlPan = true;
bfDes.ControlVolume = true;
bfDes.ControlFrequency = true;
bfDes.GlobalFocus = true;
//创建二级缓存
sbuffer = new SecondaryBuffer(FileName, bfDes, dev);
sbuffer.Volume = 0;//声音最大
sbuffer.Pan = 0;//混合声道
sbuffer.Frequency = bfDes.Format.SamplesPerSecond;//以正常速度播放
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// 播放
/// </summary>
/// <param name="PlayStyle">播放方式(正常,循环)</param>
public void Dev_Play(PlayFlags PlayStyle)
{
//开始播放
if (sbuffer != null)
{
if (sbuffer.Status.Playing == true)
sbuffer.Stop();
if (PlayStyle == PlayFlags.Default)
sbuffer.Play(0, BufferPlayFlags.Default);//正常播放
else if (PlayStyle == PlayFlags.Looping)
sbuffer.Play(0, BufferPlayFlags.Looping);//循环播放
}
}
//暂停
public void Dev_Stop()
{
if (sbuffer != null)
sbuffer.Stop();
}
//关闭
public void Dev_Dispose()
{
sbuffer.Dispose();
}
/// <summary>
/// 获取播放状态
/// </summary>
/// <returns></returns>
public DeviceStatus GetDeviceStatus()
{
DeviceStatus thisStatus = new DeviceStatus();
if (sbuffer != null)
{
BufferStatus devStates = sbuffer.Status;
thisStatus = new DeviceStatus();
thisStatus.isPlaying = devStates.Playing;//是否循环播放
thisStatus.isLooping = devStates.Looping;//是否播放
thisStatus.isTerminated = devStates.Terminated;//是否终止
}
return thisStatus;
}
}
{
SecondaryBuffer sbuffer = null;
public QBAudioCtrl(Guid DevId, string FileName)
{
try
{
//根据ID建立设备对象
Device dev = new Device(DevId);
//设置设备的优先级
dev.SetCooperativeLevel((new System.Windows.Forms.Form()), CooperativeLevel.Priority);
//设置缓存的属性
BufferDescription bfDes = new BufferDescription();
bfDes.ControlPan = true;
bfDes.ControlVolume = true;
bfDes.ControlFrequency = true;
bfDes.GlobalFocus = true;
//创建二级缓存
sbuffer = new SecondaryBuffer(FileName, bfDes, dev);
sbuffer.Volume = 0;//声音最大
sbuffer.Pan = 0;//混合声道
sbuffer.Frequency = bfDes.Format.SamplesPerSecond;//以正常速度播放
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// 播放
/// </summary>
/// <param name="PlayStyle">播放方式(正常,循环)</param>
public void Dev_Play(PlayFlags PlayStyle)
{
//开始播放
if (sbuffer != null)
{
if (sbuffer.Status.Playing == true)
sbuffer.Stop();
if (PlayStyle == PlayFlags.Default)
sbuffer.Play(0, BufferPlayFlags.Default);//正常播放
else if (PlayStyle == PlayFlags.Looping)
sbuffer.Play(0, BufferPlayFlags.Looping);//循环播放
}
}
//暂停
public void Dev_Stop()
{
if (sbuffer != null)
sbuffer.Stop();
}
//关闭
public void Dev_Dispose()
{
sbuffer.Dispose();
}
/// <summary>
/// 获取播放状态
/// </summary>
/// <returns></returns>
public DeviceStatus GetDeviceStatus()
{
DeviceStatus thisStatus = new DeviceStatus();
if (sbuffer != null)
{
BufferStatus devStates = sbuffer.Status;
thisStatus = new DeviceStatus();
thisStatus.isPlaying = devStates.Playing;//是否循环播放
thisStatus.isLooping = devStates.Looping;//是否播放
thisStatus.isTerminated = devStates.Terminated;//是否终止
}
return thisStatus;
}
}