Stop button in Speech in Php

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Stop button in Speech in Php

How are all things going CoursesWeb now with Covid-19.
It is shamble for all lovely lady`s who have the name.
I hope all Governements gonna make-up with those woman.

This script is functional but how can I add a stop button ????

Hope you can help me out....

Code: Select all

<?php
@$submit = $_POST['process'];
@$word = $_POST['texttospeech'];

$voice = new COM("SAPI.SpVoice");

if($_SERVER["REQUEST_METHOD"] == "POST" and isset($submit) and !empty($word)){
  $voice->Speak($word);
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="robots" content="noindex, nofollow">
		<title>PHP Text to Speech</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="icon" href="img/fav.png" type="image/png">
		<link href="css/bootstrap.min.css" rel="stylesheet">
		<link href="css/style.css" rel="stylesheet">
	</head>
<body>
<div class="topmost">
			<div class="col-md-12">
				<div class="panel panel-primary">
					<div class="panel-body">
						<form method = "post">
	<div class = "form-group">
<br>
<textarea class="form-control input-sm" rows="6" name="texttospeech" placeholder="Type your Text Here...">
<?php echo $users['post_text'];?>
</textarea>
<input type = "submit" class = "btn btn-primary" name="stop" value="stop">
							</div>
							<div class = "form-group">
								<input type = "submit" class = "btn btn-primary btn-block" name="process" value="Speak">
							</div>
						</form>
						<form method="post">
							<input type="hidden" name="texttospeech" value="Congratulations, you run PHP text to Speech using Microsoft Speech API. Remember, this script will run only on Microsoft Windows">
							<button type="submit" name="process" class="btn btn-danger btn-block">Click Me First</button>
						</form>
					</div>
				</div>
			</div>
		</div>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/script.js"></script>
	</body>
</html>

Admin Posts: 805
All the voice speak is outputted directly in browser. I didn't find a way to pause it.

Try to use a JS script, here is an example.

Code: Select all

<textarea cols=40 rows=4 id='queueText'>
Press "Start queue text" to add text area content to the text reader. Press it multiple times to add text more than once.

Press "pause" to pause reading.

Press "play" to start or resume reading queued text from the speech synthesizer's fifo queue. Play mode is in effect at startup - but you could pause the reader before queuing text.

Press "cancel" to stop reading and empty the queue. It does not change pause versus play mode. If the reader is paused when cancel is clicked, it remains so afterwards.

This voice is the default voice in this browser, and may be different in another. More code is needed for voice selection.

Oh, and don't forget to cancel speech synthesis on window unload.
</textarea><br>
<button onclick='tr.queue(queueText.value)'>Start queue text</button>
<p>
<button onclick='tr.pause()'>Pause</button>
<button onclick='tr.play()'>Play</button>
<button onclick='tr.cancel()'>Cancel</button>
</p>

<script>
const tr = {
	queue: null,
	pause: null,
	play:  null,
	cancel:  null,
	defaultRate: 1.1,
	defaultPitch: 1,
	// voice selection to do;
};
function createTextReader( tr) {
	let synth = window.speechSynthesis; // abbreviation
	let Utter = SpeechSynthesisUtterance; // abbreviation
	// queue
	tr.queue = (text, rate, pitch, voiceIndex) => {
		let utter = new Utter();
		utter.text = text;
		utter.rate = rate || tr.defaultRate || 1;
		utter.pitch = pitch || tr.defaultPitch || 1;
		// voice selection to do
		// if( voiceParam) ....
		synth.speak( utter);
	};
	tr.pause = () => synth.pause();
	tr.play = () => synth.resume();
	tr.cancel = () => synth.cancel();
}
window.addEventListener( 'DOMContentLoaded', function (e) {
createTextReader( tr)}, false);
window.addEventListener('unload', e=>tr.cancel(), false);
</script>
That is a code from stackoverflow, I have no experience with such applications.
For more answers, search on the net for: SpeechSynthesisUtterance js pause button, or ask on stackoverflow.

Demo:




JanMolendijk Posts: 282
I could kiss you for this support all things working & I even have it now in Dutch

Thanks Coursesweb

Similar Topics