Question

I have created a storyboard that has some moving elements/objects and I want to add a SpeechSynthesizer into a Storyboard.

Is that possible?? I am working on 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);

Or is there a way to add an audio into a storyboard?

Was it helpful?

Solution

If you are willing to use an audiofile, you can use the MediaTimeLine Class. You can then use one of the SpeachSynthesizer's SetOutputToWaveFile Methods to create your File.

Saving waveFile Modified from 2nd Link:

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

Xaml

modified from First Link for playing the file

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

Be aware that once the Storyboard plays the file it will maintain a lock on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top