HTML5 Audio Player Duration Showing Nan - html5-audio

I just started to learn HTML5 and was going through HTML5 Audio player using JQuery. I coded a player with Playlist, everything works fine except the Duration with help of which my input range slider doesn't works. When I debugged I found that my duration is showing me NAN. I am setting the duration after the song is initialized.
Here is my JS code
jQuery(document).ready(function() {
container = $('.container');
playList = $('#playlist');
playListSong = $('#playlist li');
cover = $('.cover');
play = $('#play');
pause = $('#pause');
mute = $('#mute');
muted = $('#muted');
close = $('#close');
song = new Audio('music/Whistle-Flo-Rida-Mp3-Download.mp3','music/Whistle-Flo-Rida-Mp3-Download.mp3');
var canPlay = song.canPlayType('audio/mpeg;');
if (canPlay) {
song.type= 'audio/mpeg';
song.src= 'music/Whistle-Flo-Rida-Mp3-Download.mp3';
}
playListSong.click(function(){
$('#close').trigger('click');
window["song"] = new Audio('music/'+$(this).html()+'.mp3','music/'+$(this).html()+'.mp3');
$('#play').trigger('click');
});
play.live('click', function(e) {
e.preventDefault();
song.play();
//cover.attr("title", song.duration);
$(this).replaceWith('<a class="button gradient" id="pause" href="" title=""><span>k</span></a>');
$('#close').fadeIn(300);
$('#seek').attr('max',song.duration);
});
pause.live('click', function(e) {
e.preventDefault();
song.pause();
$(this).replaceWith('<a class="button gradient" id="play" href="" title=""><span>d</span></a>');
});
mute.live('click', function(e) {
e.preventDefault();
song.volume = 0;
$(this).replaceWith('<a class="button gradient" id="muted" href="" title=""><span>o</span></a>');
});
muted.live('click', function(e) {
e.preventDefault();
song.volume = 1;
$(this).replaceWith('<a class="button gradient" id="mute" href="" title=""><span>o</span></a>');
});
$('#close').click(function(e) {
e.preventDefault();
container.removeClass('containerLarge');
cover.removeClass('coverLarge');
song.pause();
song.currentTime = 0;
$('#pause').replaceWith('<a class="button gradient" id="play" href="" title=""><span>d</span></a>');
$('#close').fadeOut(300);
});
$("#seek").bind("change", function() {
song.currentTime = $(this).val();
$("#seek").attr("max", song.duration);
});
song.addEventListener('timeupdate',function (){
curtime = parseInt(song.currentTime, 10);
$("#seek").attr("value", curtime);
});
});
When I click on playlist song, the duration is always NAN but by default I have set one song, and directly on page load when I clicked Play button then the duration works fine. It never works for playlist songs.

Use the loadedmetadata event listener to get the duration for the audio as soon as the metadata is loaded. Do something like the following code:
var audio = new Audio();
audio.src = "mp3/song.mp3";
audio.addEventListener('loadedmetadata', function() {
console.log("Playing " + audio.src + ", for: " + audio.duration + "seconds.");
audio.play();
});

Related

How to prevent audio to overlap itself when playing more than one tracks?

I have 3 link blocks and I would like, if a user clicks on two of them, to mute one of the songs.
I am unfortunately a bit novice with jQuery/Javascript, but here is the code I have right, now, which doesn't work:
<script type="text/javascript">
var isPlaying=false
$(document).ready(function(){
$('.play').click(function(){
var audioID = "sound" + $(this).attr('id');
var $this = $(this);
var music = new Audio(audio);
if (!isPlaying) {
// Not playing, let's play
isPlaying = true;
music.play();
} else {
// Stop the music
isPlaying = true;
music.pause();
};
});
});
</script>
<audio loop id="sound1" src="3.mp3"> </audio>
<audio loop id="sound2" src="2.mp3"> </audio>
<audio loop id="sound3" src="3.mp3"> </audio>
I do have another code that plays the three songs assigned to its link blocks, but they overlap.
$('.play').click(function(){
var $this = $(this);
// starting audio
var audioID = "sound" + $(this).attr('id');
$this.toggleClass('active');
if($this.hasClass('active')){
$("#" + audioID).trigger('play');
} else {
$("#" + audioID).trigger('pause');
}
Any help, or pointers in the right direction, are more than appreciated!

Hide input if mouse not moved for 5 seconds

I have a HTML img input for the page reqad.co.nf as a full screen function, how would I make this input hide if the mouse isn't moved for 5 seconds, and reappear if it moves again?<input type="image" src="fullscreenico.png" align="right" onclick="toggleFullScreen()">
Here's a snippet with a JQuery solution:
jQuery(document).ready(function(){
var lastTimeMouseMoved;
var alreadyPrompt = false;
$(document).mousemove(function(e){
lastTimeMouseMoved = new Date().getTime();
alreadyPrompt = false;
$(".theInputIdLikeToHide").show();
});
setInterval(function(){
if(!alreadyPrompt){
var currentTime = new Date().getTime();
if (currentTime - lastTimeMouseMoved > 5000) {
$(".theInputIdLikeToHide").hide();
alreadyPrompt = true;
}
}
}, 500);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="theInputIdLikeToHide">

Playing mp3 attach href link to audio control

I've created a link to play mp3 from and certain point and end at a certain point
<audio id="sample" src="Hard To Say I'm Sorry.mp3" controls preload></audio>
Play1
<script>
var audio = document.getElementById('sample');
var segmentEnd;
audio.addEventListener('timeupdate', function (){
if (segmentEnd && audio.currentTime >= segmentEnd) {
audio.pause();
}
console.log(audio.currentTime);
}, false);
function playSegment(startTime, endTime){
segmentEnd = endTime;
audio.currentTime = startTime;
audio.play();
}
</script>
</body>
</html>
My question, how do I put the href "Play1" link into the audio control?

HTML5 audio control stop button (as opposed to pause)

I've been absolutely everywhere (I think) and can't seem to find a way to call a stop event in an html5 audio controller. My audio controller has a playlist where each track will play when selected or cycle through each track to the next. There's also a next button which works.
My play/pause button looks like this:
function playPause() {
var audioPlayer = document.getElementsByTagName('audio')[0];
if(audioPlayer!=undefined) {
if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
} else {
loadPlayer();
}
}
I need a "Stop" button that stops the audio and goes back to the beginning of the track currently being played. I thought about using the current playback postion somehow, but can't get my head around that.
I've also tried this (adapted by advice here in another question), to no avail:
function stop() {
var audioPlayer = document.getElementsByTagName('audio');
addEventListener('loadedmetadata', function() {
this.currentTime = 0;
}, false);
}
Any ideas?
If your play/pause is working, this should work for stop:
function stop() {
var audioPlayer = document.getElementsByTagName('audio')[0];
audioPlayer.pause();
audioPlayer.currentTime = 0;
}
Here is a small demo code, where you can play, pause and stop an audio.
Live Demo
HTML
<input type="button" value="play" id="playBtn" />
<input type="button" value="pause" id="pauseBtn" />
<input type="button" value="stop" id="stopBtn" />
Jquery
var source = "http://www.giorgiosardo.com/HTML5/audio/audio/sample.mp3"
var audio = document.createElement("audio");
audio.src = source;
$("#playBtn").click(function () {
audio.play();
});
$("#pauseBtn").click(function () {
audio.pause();
});
$("#stopBtn").click(function () {
audio.pause();
audio.currentTime = 0;
});
Source
If you want to stop the music and abort the downloading operation you can use this code:
<button id="btnStop" onclick="player.src = ''; player.src = 'song.mp3';">Stop</button>
We are using java here to change the song src and then put it back there! Tricky but worky

enable autoplay in ipad

I am working on a project which is in html5. I am testing it in ipad. I have one background audio which play when page load. I made all code to play it, but it not play when it page load. If i click on play button then it play nicely.
Now what i want is to play audio automatically (autoplay). Below is my code,
$(document).ready(function() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'S6.mp3');
audioElement.controls = true;
audioElement.load();
audioElement.play();
audioElement.play();
$('.play').live('click',function() {
//alert('play'); // alert('asdsad');
audioElement.play();
$("#play").removeClass("play").addClass("pause");
});
$('.pause').live('click',function() {
// alert('pause'); // alert('4545');
audioElement.pause();
//$("#play").html('<a href="#" title="Play" class="play" id="play">');
$("#play").removeClass("pause").addClass("play");
});
$('.soundhover').live('click',function() {
audioElement.volume=0;
$("#off").removeClass("soundhover").addClass("soundoff");
});
$('.soundoff').live('click',function() {
audioElement.volume=1;
$("#off").removeClass("soundoff").addClass("soundhover");
});
});
autoplay doesn't work on iOS:
https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html#//apple_ref/doc/uid/TP40009523-CH5-SW1