我的程序正在.wav录制语音并将其转换为.flac
我会将此.flac文件发送给谷歌,希望我能得到语音文本
但是当我的程序尝试将文件发送到谷歌时出现错误,"该过程无法访问文件' C:\ Users \ Ahmad Mustofa \ Documents \ Visual Studio 2010 \ Projects \ FP \ voice.flac'因为它正被另一个进程使用。" 。我不知道哪个进程仍在使用该文件。
这是我的代码:
string inputFile = Path.Combine("wav ", input);//the converter
string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
if (!File.Exists(inputFile))
throw new ApplicationException("Input file " + inputFile + " cannot be found!");
WavReader wav = new WavReader(inputFile);
FlacWriter flac = new FlacWriter(File.Create(outputFile), wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
wav.Dispose();
wav = null;
//the sender
FileStream FS_Audiofile = new FileStream("C:\\Users\\Ahmad Mustofa\\Documents\\Visual Studio 2010\\Projects\\FP\\voice.flac", FileMode.Open, FileAccess.Read);
BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
FS_Audiofile.Close();
BR_Audiofile.Close();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
_HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
}
答案 0 :(得分:1)
您确定FlacWriter
会自动处理Stream
吗?你可以尝试这样的事情:
...
using (var flacStream = File.Create(outputFile))
{
FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
}
...