我正在尝试使用SpeechSynthesizer和NAduio,我的代码如下。我似乎无法使它正常工作。我收到错误消息,
该进程无法访问文件“ some file.wav”,因为它正在被另一个进程使用。
它在线上发生
wave = new WaveFileReader(path + fileName);
在synth_SpeakCompleted方法中。
我不知道该文件使用了什么?当事件触发时,SpeechSynthesizer已完成创建文件。显然我缺少了一些东西。
我之所以只保存文件,是因为我不知道如何直接从SpeechSynthesizer转到NAudio,因此,如果有不保存文件的解决方案,那就太好了。
该文件很好,并且在我手动运行时可以完美运行。
using NAudio.Wave;
public partial class MainWindow : Window
{
private WaveFileReader wave = null;
private DirectSoundOut output = null;
SpeechSynthesizer synthesizer = null;
private void DrawContent()
{
string path = @"C:\Users\MyPath\";
string fileName = "MyFile.wav";
// delete previous file if it exists
if (File.Exists(path + fileName))
File.Delete(path + fileName);
synthesizer = new SpeechSynthesizer();
synthesizer.Volume = 100; // 0...100
synthesizer.Rate = 3; // -10...10
synthesizer.SetOutputToWaveFile(path + fileName);
synthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);
synthesizer.SpeakAsync(someText);
}
void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
synthesizer.SetOutputToNull();
string path = @"C:\Users\MyPath\";
string fileName = "MyFile.wav";
wave = new WaveFileReader(path + fileName);
output = new DirectSoundOut();
output.Init(new WaveChannel32(wave));
output.Play();
}
private void DisposeWave()
{
if(output != null)
{
if (output.PlaybackState == PlaybackState.Playing)
output.Stop();
output.Dispose();
output = null;
}
if(wave != null)
{
wave.Dispose();
wave = null;
}
}
}