I want to use an event handler to stop speaking:
Speak[ExampleData[{"Text", "DonQuixoteISpanish"}]]
The only way I can stop this is by restarting mathematica!
Running Trace[Speak["Hello"]]
and Names["*Speak*"]
revealed the following possibility:
MathLink`CallFrontEnd[CurrentlySpeakingPacket]
Using this with a text that is split into a list of shorter strings allows you to interrupt the audio at well-defined points, phrase breaks, say. Here is one way to do it:
Clear[interruptibleSpeak];
interruptibleSpeak[text_String] := Module[{phrases},
phrases = StringSplit[
StringReplace[text, (# -> # <> "\n") & /@ {".", ";", "!", "?"}], "\n"];
Do[While[MathLink`CallFrontEnd[CurrentlySpeakingPacket], Pause[0.1]];
Speak[phrases[[i]]], {i, Length@phrases}]
]
You can now interrupt the computer in the usual way (Command. on Mac, Alt. on PC):
interruptibleSpeak[ExampleData[{"Text", "DonQuixoteISpanish"}]]
Now, of course, you do not get asynchronous speaking, so subsequent evaluations have to wait for the last phrase to be queued to execute.
Note This solution was tested on version 8.04, and is reported not to work on version 7.
Speak
so this is not a high priority, but I'll try to return to this later to see if it can be mended for v7.
$\endgroup$
Commented
Aug 24, 2012 at 15:07
Speak[Array["I should migrate to v8"&, 10]]
$\endgroup$
Commented
Aug 28, 2012 at 3:12
To stop the Speak function in Mathematica, you can use SpeakStop. Here is how you can use an event handler to stop the Speak function:
First, start speaking the text. Use an event handler, such as a button, to stop the speaking. Here's how you can do it:
DynamicModule[{},
Column[{
Button["Start Speaking", Speak[ExampleData[{"Text", "DonQuixoteISpanish"}]]],
Button["Stop Speaking", SpeakStop[]]
}]
In this example, there are two buttons. Clicking the "Start Speaking" button will start the Speak function, and clicking the "Stop Speaking" button will stop it.
Alternatively, if you want to use an event handler programmatically without using a button, you could define a function to stop speaking:
startSpeaking := Speak[ExampleData[{"Text", "DonQuixoteISpanish"}]]
stopSpeaking := SpeakStop[]
(* Start speaking *)
startSpeaking
(* Stop speaking after 5 seconds *)
Pause[5]
stopSpeaking
]
This script will start speaking and then stop after 5 seconds.
Speak["test test test"]; Print["hi"]
, it executes thePrint
command before the first word is even spoken. $\endgroup$Quit[]
won't have any effect because all of this is done by the FE (which is why you have to quit Mathematica to make it stop). I wonder if there is some hidden/undocumented token that can be used withFrontEndExecute
... $\endgroup$ShutUp[]
? $\endgroup$