本文介绍了C#中的.wav到.flac转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序正在以.wav录制语音并将其转换为.flac
我会将这个.flac文件发送给Google,希望我能收到语音提示.
但是当我的程序尝试将文件发送给google时出现错误,该进程无法访问文件'C:\ Users \ Ahmad Mustofa \ Documents \ Visual Studio 2010 \ Projects \ FP \ voice.flac',因为该文件正在使用另一个过程." .我不知道哪个进程仍在使用该文件.这是我的代码:

My program is recording voice in .wav and convert it to .flac
I will send this .flac file to google, hope I will get the text of the voice.
But there's error when my program trying to send the file to google, "The process cannot access the file 'C:\Users\Ahmad Mustofa\Documents\Visual Studio 2010\Projects\FP\voice.flac' because it is being used by another process." . I don't know which process that still use that file.Here is my code :

        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());
            }

推荐答案

您确定FlacWriter自动处理Stream吗?您可以尝试这样的事情:

Are you sure, that FlacWriter disposes the Stream automatically? You could try something like this:

...

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;
}

...

这篇关于C#中的.wav到.flac转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 07:01