I am trying to load/download the audio file from the URL, create an HTML audio tag, and play it. Everything works fine. The audio will download without any issues from the URL and then play automatically, but when I click on the Pause button, the audio will start to play from the beginning instead of stopping/pausing!
function load_Audio( contentID ) {
var audioUrl = $( contentID ).text();
$.ajax({
url: audioUrl,
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function(data) {
// Create a blob URL for the audio file
var blobUrl = URL.createObjectURL(data);
const audioElement = document.createElement('audio');
audioElement.setAttribute('controls', true);
audioElement.setAttribute('controlsList', 'nodownload');
audioElement.setAttribute('autoplay', 'autoplay');
document.body.appendChild(audioElement);
const sourceElement = document.createElement('source');
audioElement.appendChild(sourceElement);
sourceElement.src = blobUrl;
sourceElement.type = 'audio/mpeg';
$('#audioholder').html(audioElement);
},
error: function() {
$('#audioholder').html('Failed to load the audio file');
}
});
}
So, what's the issue here? What's the solution to solve this pause/stop issue?
I am trying to load/download the audio file from the URL, create an HTML audio tag, and play it. Everything works fine. The audio will download without any issues from the URL and then play automatically, but when I click on the Pause button, the audio will start to play from the beginning instead of stopping/pausing!
function load_Audio( contentID ) {
var audioUrl = $( contentID ).text();
$.ajax({
url: audioUrl,
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function(data) {
// Create a blob URL for the audio file
var blobUrl = URL.createObjectURL(data);
const audioElement = document.createElement('audio');
audioElement.setAttribute('controls', true);
audioElement.setAttribute('controlsList', 'nodownload');
audioElement.setAttribute('autoplay', 'autoplay');
document.body.appendChild(audioElement);
const sourceElement = document.createElement('source');
audioElement.appendChild(sourceElement);
sourceElement.src = blobUrl;
sourceElement.type = 'audio/mpeg';
$('#audioholder').html(audioElement);
},
error: function() {
$('#audioholder').html('Failed to load the audio file');
}
});
}
So, what's the issue here? What's the solution to solve this pause/stop issue?
The fix is to use the Audio()
constructor instead of document.createElement('audio')
.
The Audio()
constructor creates a more stable audio object that handles blob URLs better in Safari and Its properly maintains the audio state and timing, allowing pause/play to work as expected
Try this solution:
// Instead of this:
const audioElement = document.createElement('audio');
// Use this:
const audioElement = new Audio();
load_Audio()
? I'm guessing it's in an event listener that gets triggered again when the user clicks on the Pause button. – Barmar Commented Jan 30 at 22:02snake_case
orcamelCase
. Mixing both in the same identifier is weird. – Barmar Commented Jan 30 at 22:03src
attribute for the audio element, and then the browser will handle streaming for you. Much more efficient. – Brad Commented Jan 31 at 4:06