On my WordPress site I have a form that converts text to audio using AJAX and PHP. The file name remains the same when the converted text is updated, it just recreates the file.
Everything seems to work fine except that the audio source to be played is not being reloaded/refresh when I update the text.
The default download button gives me the updated file and if I refresh the whole page, I can only then hear the updated file playing.
My code (simplified for relevance) --
jQuery(document).on('click', '#savetext', function(e) {
e.preventDefault();
var myVideo = $('#player');
var path = $("#path").val();
myVideo.src = path;
$.ajax({
data: {
action: 'polly_pros',
pollytext: txt,
rate: rate,
voice: voice
},
type: 'post',
url: polpro.ajax_url,
cache: false,
success: function(data) {
console.log(data);
myVideo.load();
myVideo.get(0).play();
},
error: function() {
console.log("Error");
}
});
});
<audio id="player" controls>
<source id="audiosource" src="<?php echo $thepath; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<textarea name="pollytext" id="pollytext"></textarea>
<button type="submit" id="savetext" name="savetext">save text</button>
How can I make it so that when the file is updated, so is the audio that plays?
UPDATE
I tried the following suggestion but it didnt fix the issue -
$.ajax ({
data: {
action: 'polly_pros',
pollytext: txt,
rate: rate,
voice: voice
},
type: 'post',
url: polpro.ajax_url,
cache: false,
success: function(data) {
console.log(data);
myVideo.src = path+"&rnd="+new Date().getTime();
myVideo.load();
myVideo.get(0).play();
},
error: function() {
console.log("Error");
}
});