Ajax play audio after running successfully

Asked

Viewed 190 times

3

I need to play an audio in the execution of the excerpt below, where it displays the data found in a query:

// Verifica se o Ajax realizou todas as operações corretamente
if(req.readyState == 4) {
    if (req.status == 200) {
        // Resposta retornada pelo PesquisaRomaneio.php
        var resposta = req.responseText;

        // Abaixo colocamos a(s) resposta(s) na div resultado
        document.getElementById('resultado').innerHTML = resposta;
        // Toca audio se encontrou
        var snd = new Audio("../images/alert.mp3"); 
        snd.play();
    } else {
        document.getElementById('resultado').innerHTML = "Erro: " + req.status;
    }

    setTimeout(buscarRomaneio, segundos * 1000);
}

I tried to use the code below but did not execute. How can I fix this?

 var snd = new Audio("../images/alert.mp3"); 
 snd.play();
  • 1

    What is your browser? Tested in other browsers?

  • 2

    Make sure the audio is in the folder images?

  • I tested on FF, Chrome, Opera and IE. None worked. The audio is in the images folder, for testing... I tried to drop it at the root, it didn’t work either.

  • 1

    It works if you put in some folder that doesn’t start with ..?

  • no, I put in the folder that is the js: var snd = new audio("Alert.mp3"); and still did not play...

1 answer

0


Solved as follows (FF, Chrome, Opera):

// Verifica se o Ajax realizou todas as operações corretamente
if(req.readyState == 4) {
    if (req.status == 200) {
        // Resposta retornada pelo PesquisaRomaneio.php
        var resposta = req.responseText;

        // Abaixo colocamos a(s) resposta(s) na div resultado
        document.getElementById('resultado').innerHTML = resposta;
        // Toca audio se encontrou
        var snd = new Audio("sounds/alert.mp3"); 
        snd.load();
        snd.play();
    } else {
        document.getElementById('resultado').innerHTML = "Erro: " + req.status;
    }

    setTimeout(buscarRomaneio, segundos * 1000);
}

In IE it didn’t work that way (the go...). So in PHP file, I put it that way:

while (OCIFetch($consulta)){
    if (!empty($v_romaneio)) {
        echo "<td><b>" . $v_romaneio . "</b></td><br />";
        echo "<embed src='images/alert.mp3'width='1' height='1'>";
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.