Dispose object Speakasync to wav stream

时间:2016-04-25 09:12:23

标签: c# audio

My code creates a power point presentation and three audio files. I want to know the length of those audio files after the presentation is created so I use:

            double duration = 0;
            WindowsMediaPlayer wmp = new WindowsMediaPlayer();
            IWMPMedia mediainfo = wmp.newMedia(file);
            duration = mediainfo.duration;
            wmp.close();
            return duration;

To create the audio files I use

public void CreateAudio(string text)
            {
                y++;
                synth = new SpeechSynthesizer();
                AudioStream = new FileStream(folder + @"\audio\a" + @y.ToString() + @".wav", FileMode.OpenOrCreate);
                //synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth)
                synth.SetOutputToWaveStream(AudioStream);
                synth.SpeakAsync(text);                
            }
            private void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
            {
                synth.Dispose();
            }

The problem is that after the presentation is created, only the shortest audio file returns a length, the rest return 0. If I check manually, I see 3 audio files with a valid length property, but the program doesn't read that for some reason. My attempt at disposing the synth didn't change a thing, nor did using syth.speak so I must have done something terribly wrong at using and managing my objects and memory but I don't know what or where.

If I use the code to check the length of the audio files created in a different code for example, it works perfectly, just when I create them and want to check their length something goes wrong.

0 个答案:

没有答案