知识问答

C#使用DirectX.DirectSound播放语音

下面我就详细讲解一下C#使用DirectX.DirectSound播放语音的完整攻略。

1.准备工作

在开始使用DirectX.DirectSound播放语音之前,需要先安装Microsoft DirectX SDK,并将其添加到工程引用中。

2.创建DirectSound实例

使用DirectX.DirectSound播放语音的第一步是创建DirectSound的COM对象。创建方式如下:

using Microsoft.DirectX.DirectSound;// ...DirectSound sound = new DirectSound();

3.设置音频格式

在使用DirectX.DirectSound播放语音之前,需要知道需要播放的音频的格式,因为DirectSound需要知道音频的格式才能正确地播放声音。音频的格式包括采样率、声道数、比特数等。创建音频格式如下:

WaveFormat waveFormat = new WaveFormat();waveFormat.FormatTag = WaveFormatTag.Pcm;waveFormat.SamplesPerSecond = 44100;waveFormat.BitsPerSample = 16;waveFormat.Channels = 1;waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample / 8));waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * waveFormat.SamplesPerSecond;

上述代码创建了一个采样率为44100Hz,1个声道,每个声道的比特数为16的音频格式。

4.创建音频缓冲区

接下来,需要创建一个音频缓冲区来存储要播放的声音数据。创建音频缓冲区的方式如下:

BufferDescription bufferDesc = new BufferDescription(waveFormat);bufferDesc.GlobalFocus = true;bufferDesc.ControlVolume = true;bufferDesc.ControlPan = true;bufferDesc.ControlFrequency = true;bufferDesc.ControlEffects = true;SecondaryBuffer soundBuffer = new SecondaryBuffer(waveFilePath, bufferDesc, sound);

这里的 waveFilePath 是要播放的音频文件的路径。BufferDescription 类用于描述缓冲区的属性,包括全局聚焦、控制音量、控制平衡、控制频率以及控制效果等。SecondaryBuffer 类用于创建缓冲区。

5.播放声音数据

一旦创建了音频缓冲区,就可以开始播放声音数据了。可以使用 Play 方法播放声音:

soundBuffer.Play(0, BufferPlayFlags.Default);

在此代码中,第一个参数是指要开始播放的位置(以字节为单位),第二个参数是播放标志,这里使用默认值。使用 Stop 方法停止声音播放:

soundBuffer.Stop();

示范一

接下来是一个示例,该示例使用 System.Media.SoundPlayer 类播放一个简单的音频文件 sound.wav

using System.Media;// ...SoundPlayer soundPlayer = new SoundPlayer(@"C:\sound.wav");soundPlayer.Play();

示范二

下面是一个更复杂的示例,用于播放多个音频文件。首先,将需要播放的音频文件放入一个 List<string> 中:

List<string> audioFiles = new List<string>(){    @"C:\audio1.wav",    @"C:\audio2.wav",    @"C:\audio3.wav"};

然后,使用 DirectX.DirectSound 播放这些音频文件:

using Microsoft.DirectX.DirectSound;// ...DirectSound sound = new DirectSound();sound.SetCooperativeLevel(this, CooperativeLevel.Priority);foreach (string audioFile in audioFiles){    WaveFormat waveFormat = new WaveFormat();    waveFormat.FormatTag = WaveFormatTag.Pcm;    waveFormat.SamplesPerSecond = 44100;    waveFormat.BitsPerSample = 16;    waveFormat.Channels = 1;    waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample / 8));    waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * waveFormat.SamplesPerSecond;    BufferDescription bufferDesc = new BufferDescription(waveFormat);    bufferDesc.ControlEffects = false;    bufferDesc.ControlFrequency = true;    bufferDesc.ControlPan = false;    bufferDesc.ControlVolume = true;    bufferDesc.GlobalFocus = true;    SecondaryBuffer soundBuffer = new SecondaryBuffer(audioFile, bufferDesc, sound);    soundBuffer.Play(0, BufferPlayFlags.Default);    while (soundBuffer.Status.BufferBytes != 0)    {        Application.DoEvents();        if (soundBuffer.Status.Playing == false)        {            break;        }    }}

代码中使用 CooperativeLevel.Priority 设置协作等级以确保播放声音不会被其他应用程序打断。其中的 BufferDescription 中关闭了 ControlEffectsControlPan,只开启了 ControlVolumeControlFrequency。在遍历 audioFiles 时,分别创建了 SecondaryBuffer 对象,并分别调用 Play 方法播放声音,然后在循环中等待声音播放完毕。