Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 03:26 PM   PM User | #1
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
Javascript Audio Player

I am currently in the process of developing a website with a custom audio player that will use specified divs as their controllers. I am currently confused on a few things:
1. Getting the next song to play after the first one is done
2. Implementing a playlist.

For number one, I have implemented the following JS for the play button:
Code:
$("#play-button").click(function(){
		if (currentsong = 1) {
			$("#audio-1")[0].play();
		}
		else if (currentsong = 2) {
			$("#audio-2")[0].play();
		}
		else if (currentsong = 3) {
			$("#audio-3")[0].play();
		}
		else if (currentsong = 4) {
			$("#audio-4")[0].play();
		}
		else {
			$("#audio-5")[0].play();
		}
	})
However, this doesn't solve the issue of getting the next song to play after. Furthermore, is there a more efficient way of implementing the code above?

So I guess to specify, my plea for help encompasses the following points:
1. Implementation of a playlist
2. Getting the next song to play after the preceeding song has finished
3. [much more general] Any other methods to create a playlist using javascript

Thanks,
xdragon124 is offline   Reply With Quote
Old 11-14-2012, 03:41 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
1) Where is currentsong defined?
2) Where do you update currentsong?
3) The comparison operator is == rather than =
Code:
if(currentsong == 1) {
devnull69 is offline   Reply With Quote
Old 11-14-2012, 05:41 PM   PM User | #3
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
Code:
$(document).ready(function(){
	var currentsong;
	
	$("#play-button").click(function(){
		if (currentsong == 1) {
			$("#audio-1")[0].play();
		}
		else if (currentsong == 2) {
			$("#audio-2")[0].play();
		}
		else if (currentsong == 3) {
			$("#audio-3")[0].play();
		}
		else if (currentsong == 4) {
			$("#audio-4")[0].play();
		}
		else {
			$("#audio-5")[0].play();
		}
	}) 
	
	$("#pause-button").click(function(){
		if (currentsong == 1) {
			$("#audio-1")[0].pause();
		}
		else if (currentsong == 2) {
			$("#audio-2")[0].pause();
		}
		else if (currentsong == 3) {
			$("#audio-3")[0].pause();
		}
		else if (currentsong == 4) {
			$("#audio-4")[0].pause();
		}
		else {
			$("#audio-5")[0].pause();
		}
	})
	
	$("#next-button").click(function(){
		if (currentsong < 5) {
			currentsong = currentsong + 1;
		}
		else if (currentsong == 5) {
			currentsong = 1;
		}
	})
})
this is the entire file currently.

I edited the comparison operators from = to ==, but now the buttons no longer function.

Thanks,

Last edited by xdragon124; 11-14-2012 at 06:03 PM.. Reason: Adding contextual information
xdragon124 is offline   Reply With Quote
Old 11-14-2012, 06:58 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This is a much shorter equivalent version (with added initialisation for currentsong)
Code:
$(document).ready(function(){
	var currentsong=1;
	
	$("#play-button").click(function(){
                $('#audio-'+currentsong)[0].play();
	}) 
	
	$("#pause-button").click(function(){
                $('#audio-'+currentsong)[0].pause();
	})
	
	$("#next-button").click(function(){
		if (currentsong < 5) {
			currentsong++;
		}
		else {
			currentsong = 1;
		}
	})
})

Last edited by devnull69; 11-14-2012 at 07:09 PM..
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
xdragon124 (11-15-2012)
Old 11-15-2012, 04:54 AM   PM User | #5
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
Thank you very much devnull69. The player now works as I hoped.

I have another concern regarding the audio player. I would like to set it so that after one song finishes, the next song will play. Is there a way I can implement this?

Thanks,

Last edited by xdragon124; 11-15-2012 at 04:57 AM..
xdragon124 is offline   Reply With Quote
Old 11-15-2012, 08:32 AM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
The specification says that there is the "ended" event for <audio> elements that should do exactly what you were looking for.

But: Last time I checked (about one year ago) it wasn't implemented in all browsers. Maybe it's different now, try it
Code:
$('#audio-1').on('ended', function() {
   $('#audio-2')[0].play();
});
devnull69 is offline   Reply With Quote
Old 11-15-2012, 05:47 PM   PM User | #7
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
Hi,
I have tested that and it indeed works. Thanks for everything.

One last thing, I would like to make a moving banner where the current song name would go. Is there any way I can do this?

I could make a string variable where each value of currentsong would denote a song title. But I'm at a lost.
xdragon124 is offline   Reply With Quote
Old 11-17-2012, 12:04 AM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by xdragon124 View Post
Hi,
I have tested that and it indeed works. Thanks for everything.

One last thing, I would like to make a moving banner where the current song name would go. Is there any way I can do this?

I could make a string variable where each value of currentsong would denote a song title. But I'm at a lost.
i used a marquee tag for that old-school winamp-style scrolling title.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
xdragon124 (11-21-2012)
Old 11-21-2012, 04:35 AM   PM User | #9
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
I have implemented the marque tag as suggested.
However, the text will not fully scroll through before it cuts off and starts from the start again.

Last edited by xdragon124; 11-21-2012 at 05:11 AM..
xdragon124 is offline   Reply With Quote
Old 11-21-2012, 09:29 AM   PM User | #10
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by xdragon124 View Post
I have implemented the marque tag as suggested.
However, the text will not fully scroll through before it cuts off and starts from the start again.
adjust the font size, or put the marquee at width>100% inside a container whose width is set to the proper size and whose oveflow property is "hidden".

look into the different attrib supported by marquee; you can adjust the scroll behavior and speed. i don't know the options off-hand or whether or not they will help you. i seem to rembeber being able to tell it to loop or run once, perhaps all you need to do is loop it...

anyways, glad its working more than before; progress.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 11-21-2012, 03:04 PM   PM User | #11
xdragon124
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
xdragon124 is an unknown quantity at this point
Hmm, alright, thanks I look over the documentation!

One last question remaining this: I'm using cufon for font replacement. When it first loads up, the font is successfully replaced, however, as it moves to the next song, the text goes back to the default Times New Romans. Is there a way that I can actively update the font replacement? Thanks,
xdragon124 is offline   Reply With Quote
Old 11-21-2012, 03:20 PM   PM User | #12
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by xdragon124 View Post
Hmm, alright, thanks I look over the documentation!

One last question remaining this: I'm using cufon for font replacement. When it first loads up, the font is successfully replaced, however, as it moves to the next song, the text goes back to the default Times New Romans. Is there a way that I can actively update the font replacement? Thanks,
i would dump the cufon and use webfonts, which extend css with new font-families.

i haven't implemented cufon myself, but my design guy moved to web fonts last year, after chrome and firefox removed some pretty asinine SOP restrictions from it...

i'm sure cufon provides a javascript command to re-scan, but you'll have to figure that one out on your own, show/hide many marquees, use webfonts, or repost a dedicated cufon question...

godspeed
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
xdragon124 (11-22-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:59 AM.


Advertisement
Log in to turn off these ads.