Hi!

I have a page, and one part of this page (javascript) reads the duration of song. But what if user's javascript installed on computer doesn't support this? Do all javascript support this function?
Here it is:

//for some browser this function (like IE):
duration = document.sound.currentMedia.durationString;
//and for other this (like Firefox):
duration = document.embeds['sound'].GetDuration();
scale = document.embeds['sound'].GetTimeScale();
duration = Math.floor(duration*(1/scale));

I believe that the second version should be just document.embeds['sound'].GetDuration() / document.embeds['sound'].GetTimeScale() , but just test it in all browsers to see if it works. (It should.)

It doesn't work in opera.
PS. But I don't know is this code not working or something else (it's a function), so, here's the function:

function display_time()
{
	// This function not only displays the current position
	// of time in the file, but it also get's the end time
	// so that if a song reaches the end it will play the
	// next song
	var time;
	var duration;
	var song_status;
	var buffer_status; // used for media player
	var mins; // used for quicktime
	var secs; // used for quicktime
	var scale; // used for quicktime
	
	// if IE or Netscape then Media Player 
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
	{
		//document.getElementById('message').innerHTML = typeof document.sound.settings;
		if (!document.sound.settings)
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support autoplay of next song in playlist.';
		return; // done because no fade support
	}		
		//song_status = document.sound.status;
		song_status = document.sound.playState;
		buffer_status = document.sound.network.bufferingProgress;
		
		
	if (buffer_status < 100) // 3 = playing; 1 = stopped
	{	
		t2 = setTimeout('display_time();', 20); // recall this function in 20ms
		return;
	}
	else 
	{
		if (document.getElementById('songtime'))
			document.getElementById('songtime').innerHTML = 'Buffering: '
			+ buffer_status + '%'; 
	}			
		//time = document.sound.controls.currentPosition;
		time = document.sound.controls.currentPositionString;
		//duration = document.sound.currentMedia.duration;
		duration = document.sound.currentMedia.durationString;
		
	}	
	else  // Firefox or Safari
	{
	
	// if quicktime GetVolume not supported
	if (typeof document.sound.GetVolume != 'function')
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support autoplay of next song in playlist.';
		return ; // done 
	}
	
	song_status = document.embeds['sound'].GetPluginStatus();
	

	if (song_status.toLowerCase() != 'playable' &&
		song_status.toLowerCase() != 'complete')
	{	
		t2 = setTimeout('display_time();', 250); // recall this function every 250 ms
		return;
	}
	else 
	{
		if (document.getElementById('songtimesecond'))
		document.getElementById('songtimesecond').innerHTML = song_status;
	} 
	
	time = document.embeds['sound'].GetTime();
	duration = document.embeds['sound'].GetDuration();
	scale = document.embeds['sound'].GetTimeScale();
	time = Math.floor(time*(1/scale));  // convert to seconds
	duration = Math.floor(duration*(1/scale));  // converts to seconds
	// convert seconds into mm:ss
	mins = Math.floor(time / 60);
	secs = time - (mins * 60);	
	time = mins + ':' + secs;
	mins = Math.floor(duration / 60);
	secs = duration - (mins * 60);	
	duration = mins + ':' + secs;
	
	
	}
	
	if (document.getElementById('songtime')){
		time2=time.split(":");
		time_mins_trenutno=time2[0];
		time_secs_trenutno=time2[1];
		time_all_trenutno=(time_secs_trenutno*1)+(time_mins_trenutno*60);
		duration2=duration.split(":");
		time_mins_ukupno=duration2[0];
		time_secs_ukupno=duration2[1];
		time_all_ukupno=(time_secs_ukupno*1)+(time_mins_ukupno*60);
		time_trenutno_postotak=time_all_trenutno*100/time_all_ukupno;
		width_songtimebar=time_trenutno_postotak*2;
		time_back = (time_all_ukupno*1)-(time_all_trenutno*1);
		mins_back = Math.floor(time_back/60);
		secs_back = time_back - (mins_back*60);
		document.getElementById('songtimemins').innerHTML = '<b>-'+mins_back+'</b>';
		document.getElementById('songtimesecs').innerHTML = '<b>'+secs_back+'</b>';
		document.getElementById('songtimebar').innerHTML="<div id='songtimebar_done' style='background-color:black;height:5px;'></div>"
		document.getElementById('songtimebar_done').style.width=width_songtimebar;
	}

	// This is for Firefox
	if (time == duration) // if at end of song
	{	
		next_song(3); // then play next song in playlist
		return;
	}
	// This is for Ie
	if (song_status == 1) // song stopped
	{	
		next_song(3);
		return;
	}
	t2 = setTimeout('display_time();', 250); // recall this function every 250ms
	
} // end function display_time()

If you want it to work in Opera, assign an ID to the element and use

var song = document.getElementById('that_id');
var duration = song.GetDuration() / song.GetTimeScale();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.