1

I am making a talking bot atm. But sound files overtaking each other. They playing at the same time. I can't handle this. How can i play them in order, when one finishes, then another should play... Then my bot could talk... Can you give me an example of code please? Thanks...

Here is my code...

What i want is, the audio should tell me "Meh" or something like that. I want it to be talking...

Thanks!

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form action="" method="post">
    <input name="deger" type="text" value="">
    <input type="submit">
</form>

<?php 

$m="m";
$e="e";
$r="r";
$h="h";
$a="a";
$b="b";

if (strpos($_POST["deger"], $m) !== false) {
    echo "<script>var birdSound = new Audio('m.wav');
birdSound.loop = false;
birdSound.play();
</script>";

} 
//
if (strpos($_POST["deger"], $e) !== false) {
    echo "<script>var birdSound = new Audio('e.wav');
birdSound.loop = false;
birdSound.play();
</script>";

} 
//

if (strpos($_POST["deger"], $h) !== false) {
    echo "<script>var birdSound = new Audio('h.wav');
birdSound.loop = false;
birdSound.play();
</script>";

}
?>


</body>
</html>
  • See http://stackoverflow.com/questions/42049639/preload-mp3-file-in-queue-to-avoid-any-delay-in-playing-the-next-file-in-queue/ – guest271314 May 12 '17 at 16:02

1 Answers1

0

This is the basic idea to enqueue the playing: Using the "ended" event to know when the current audio has finished.

var audioFiles= ["a.wav", "b.wav", "c.wav", ... ];

function playNext() {
  if (audioFiles.length) {
    var sound = new Audio(audioFiles.shift());
    sound.addEventListener("ended", playNext);
  }
}

playNext();
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • But i want to it work with my php script. When i type m,e and h, javascript should understand that i typed following (m,e,h) characters and bot should say it as (MEH)... I can't found a way to do this... I don't want list of wav audios to play, i want scripted and programmed audio files play in order and sequence... Is this possible? – Ozan Şiar Palik May 12 '17 at 16:18