Ollie Fulleylove
Ollie Fulleylove

Reputation: 43

Dynamically Load Audio sound Javascript

I am trying to load an audio file which changes each time a request is sent to my PHP page. For some reason, my site does not like to open the new version of the file.

function reloadFile(command) {
    $.post("function.php", { data: command }, function(data) {
        console.log("Sever Says : " + data);
    }).done(
    function(data){             
        // Error In Here
        var audio = new Audio("a.ogg");
        audio.play();   
    });
}

Upvotes: 1

Views: 2425

Answers (2)

Ollie Fulleylove
Ollie Fulleylove

Reputation: 43

I appreciate all of your help eventually I came to the conclusion that I would need to use the current server time to request the newly edited file in Apache and PHP.

The new code is as follows

var url = "filename.mp3?cb=" + new Date().getTime();
var audio = new Audio(url);
audio.load();
audio.play();

Upvotes: 1

magoeke
magoeke

Reputation: 101

I think the problem is that the Browser only has the old audio loaded. You also need to send the changed audio file to the browser.

Upvotes: 0

Related Questions