SpeechSynthesizer进入Storyboard

时间:2013-07-29 01:09:36

标签: c# storyboard speechsynthesizer

我创建了一个有一些移动元素/对象的故事板,我想将一个SpeechSynthesizer添加到一个Storyboard中。

可能吗?我正在研究C#。

Storyboard myStoryboard=new Storyboard();

SpeechSynthesizer reader = new SpeechSynthesizer();  
reader.Speak("This is my first speech project"); /* instead of speak I want 
                                                    to add this into the storyboard
.....

myStoryboard.Children.Add(readerAnimation);

或者有没有办法将音频添加到故事板?

1 个答案:

答案 0 :(得分:2)

如果您愿意使用音频文件,则可以使用MediaTimeLine类。然后,您可以使用SpeachSynthesizer's SetOutputToWaveFile方法之一来创建文件。

保存第二个链接修改的waveFile:

using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    synth.SetOutputToWaveFile(@"C:\temp\Sample.wav");
    PromptBuilder builder = new PromptBuilder();
    builder.AppendText("Hello World !");
    synth.Speak(builder);
}

Xaml

从First Link修改以播放文件

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <StackPanel Background="Black">
        <Label Name="clickMe" Content="Click Me" Foreground="White" FontFamily="Arabic Typesetting" FontSize="20" HorizontalContentAlignment ="Center"   />
        <MediaElement Name="myMediaElement"  Width="0" Height="0" />
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.MouseDown" SourceName="clickMe">
                <EventTrigger.Actions>
                    <BeginStoryboard Name= "myBegin">
                        <Storyboard x:Name="myStoryBoard" SlipBehavior="Slip">
                            <MediaTimeline Source="C:\temp\Sample.wav" Storyboard.TargetName="myMediaElement" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </StackPanel.Triggers>
    </StackPanel>
</Window>

请注意,一旦故事板播放该文件,它将保持锁定。