2013-07-29 26 views
2

我創建了一個有一些移動元素/對象的故事板,我想將SpeechSynthesizer添加到Storyboard中。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); 

或者有沒有辦法將音頻添加到故事板?

+0

SpeechSynthesizer我不是一個框架元素,所以你不能動畫它,你只是想找一種方法來動畫屏幕上的文字? –

+0

不需要。我想將SpeechSynthesizer加入故事板。 基本上我有一個包含許多動畫的故事板。我還想添加SpeechSynthesizer。 – galaxias

+0

和你期望會發生什麼,即使你可以添加它什麼都不會做,SpeechSynthesizer只是一個類, –

回答

2

如果您願意使用audiofile,則可以使用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

從第一鏈接修改播放文件

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

注意,一旦故事板播放文件時,它將保持鎖定。