Ujjwal Raina
Ujjwal Raina

Reputation: 31

How Can I pause speak command in vbscript? I have to play it from the same paused position

  1. How Can I pause speak command in vbscript? I have to play it from the same paused position.
  2. Code block:

    Dim Speak, Path
    Path = "string"
    Path = "C:\Users\sony\Desktop\TheReunion.txt"
    const ForReading = 1
    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
    strFileText = objFileToRead.ReadAll()
    Set Speak=CreateObject("sapi.spvoice")
    Speak.Speak strFileText
    objFileToRead.Close
    Set objFileToRead = Nothing
    

Upvotes: 3

Views: 717

Answers (2)

Albert F D
Albert F D

Reputation: 529

Intrigue in this led me to take inspiration from Kira's answer and develop it somewhat (in a bad, novice kind of way), to achieve the pause/resume objective interactively, the code below works for me, and hopefully it's of some help to you...

option explicit
dim strpath, fso, strfile, strtxt, user, voice, flag

flag = 2

call init
sub init
do while len(strpath) = 0
strpath = inputbox ("Please enter the full path of txt file", "Txt to Speech")
if isempty(strpath) then
wscript.quit()
end if
loop
'strpath = "C:\Users\???\Desktop\???.txt"

set fso = createobject("scripting.filesystemobject")
on error resume next
set strfile = fso.opentextfile(strpath,1)
if err.number = 0 then
strtxt = strfile.readall()
strfile.close
call ctrl
else
wscript.echo "Error: " & err.number & vbcrlf & "Source: " & err.source & vbcrlf &_
"Description: " & err.description
err.clear
call init
end if
end sub

sub ctrl
user = msgbox("Press ""OK"" to Play / Pause", vbokcancel + vbexclamation, "Txt to Speech")
select case user
case vbok
    if flag = 0 then
        voice.pause
        flag = 1
        call ctrl
    elseif flag = 1 then
        voice.resume
        flag = 0
        call ctrl
    else
        call spk
    end if
case vbcancel
    wscript.quit
end select
end sub 

sub spk
'wscript.echo strtxt
set voice = createobject("sapi.spvoice")
voice.speak strtxt,1
flag = 0
call ctrl
end sub

Upvotes: 1

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

You need to call the speak method asynchronously before using the pause and resume methods as mentioned by LotPings in the comments.

Code:

Dim Speak, Path
Path = "string"
Path = "C:\Users\sony\Desktop\TheReunion.txt"
const ForReading = 1
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
strFileText = objFileToRead.ReadAll()
Set Speak=CreateObject("sapi.spvoice")
Speak.Speak strFileText,1                          '1=Asynchronous. Click the link below for other possible values "SpeechVoiceSpeakFlags"

'Due to async call to speak method, we can proceed with the code execution while the voice is being played in the background. Now we can call pause and resume methods
wscript.sleep 5000                 'voice played for 5000ms
Speak.pause                        'paused
wscript.sleep 4000                 'remains paused for 4000ms 
Speak.resume                       'resumes 
objFileToRead.Close
Set objFileToRead = Nothing

SpeechVoiceSpeakFlags

Upvotes: 2

Related Questions