Welcome to the next installment of "scripting by dummies". In this episode we will be looking at a little script snippet the plays a wav file using SAPI.SpVoice and SAPI.SpFileStream objects. If you don't know what they are... don't worry that won't keep you from using them. I'm assuming at this point that you have read the previous three episodes and have a rough understanding of how these scripts work so let me push you right into the deep end on this one and pop the code right up there for all to see:
Code: Select all
Set oVoice = CreateObject("SAPI.SpVoice")
set oSpFileStream = CreateObject("SAPI.SpFileStream")
oSpFileStream.Open "Sounds\Speech_on.wav"
oVoice.SpeakStream oSpFileStream
oSpFileStream.Close
Line 3 tells the SAPI.SpFileStream that we named "oSpFileStream" to open "Sounds\Speech_on.wav". "Sounds\Speech_on.wav" could just as easily say "c:\mysounds\fsx\cockpit\buttonclick.wav", but it does have to be a wav file mp3's won't work.
Can anyone tell me what I left out?? Did you notice how the first object we created was SAPI.SpVoice I sure noticed when I first saw it.
Code: Select all
oVoice.Speak "I have finished playing the wave file. Will there be anything else?"
Here we can see two things. The first is that you can use the windows text to speech engine (wicked cool) and the second is that you can re-use objects. Bear in mind each object has it's own capabilities and limitations. They are not just "do anything magic boxes". What I mean is, a dog can dig in the garbage, and chase parked cars... be he can't fly... for that you need to create a "bird" object.
Incidentally if you only want the text to speech portion of the script, what lines of code do you think you might need? Well the "Boss it around part that does the talking is "oVoice.Speak" right? so we would need first create the oVoice object and what kind of object does the speaking? "SAPI.SpVoice" so the code looks like this.
Code: Select all
Set oVoice = CreateObject("SAPI.SpVoice")
oVoice.Speak "I have finished playing the wave file. Will there be anything else?"
Till next time