Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Music player problem 1

Status
Not open for further replies.

Jonnn

Programmer
Nov 17, 2004
135
GB
Hello, i have a problem with my music player.

I want to add like a playlist to my music player. But i cannot find a way of changing the song to another when you press the "next" button.

I have tried everything.

Made an array

var x=0;

var music_choice = new Array();
music_choice[x] = "music1.wav";
music_choice[x] = "music2.wav";

and then using NEXT to x++ and PREVIOUS to x--

ive tried everything


maybe some tips?

Thanks guys!



JavaScript Beginner at work :)
 
Code:
function alter_player(what_to_do) {

	if(what_to_do == "play") {
	document.use_this.play();
	document.getElementById("chosen_action").innerHTML = "Play";
	}

	if(what_to_do == "stop") {
	document.use_this.stop();
	document.getElementById("chosen_action").innerHTML = "Stop";	
	}
}

Code:
var music = new Array();

music[0] = "nirvana-comeasyouare.wav";
music[1] = "nirvana-lithium.wav":

JavaScript Beginner at work :)
 
The reason your next and previous buttons are not working is because you've not written any code to handle their commands being passed into the "alter_player" function.

Try something like this for size (untested), with your array code above:

Code:
var songNum = 0;
function alter_player(what_to_do) {
	var myPlayer = document.getElementById('use_this');

	if(what_to_do == 'play') {
		myPlayer.play();
		document.getElementById("chosen_action").innerHTML = 'Play';
	}

	if(what_to_do == 'stop') {
		myPlayer.stop();
		document.getElementById("chosen_action").innerHTML = 'Stop';    
	}

	if(what_to_do == 'next') {
		myPlayer.stop();
		songNum++;
		if (songNum == music.length) songNum = 0;	// If we've reached past the last song, loop around to the first
		myPlayer.src = music[songNum];
		myPlayer.play();
		document.getElementById("chosen_action").innerHTML = 'Playing song ' + (songNum+1) + ' of ' + music.length;
	}

	if(what_to_do == 'previous') {
		myPlayer.stop();
		songNum--;
		if (songNum == -1) songNum = music.length-1;	// If we've reached past the first song, loop around to the last
		myPlayer.src = music[songNum];
		myPlayer.play();
		document.getElementById("chosen_action").innerHTML = 'Playing song ' + (songNum+1) + ' of ' + music.length;
	}
}

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hmm - OK - you should also include a full path to the music file for it to work, I imagine. so change both occurrences of this:

Code:
myPlayer.src = music[songNum];

to this:

Code:
myPlayer.src = '[URL unfurl="true"]http://www.freewebs.com/foxandhounds/'[/URL] + music[songNum];

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
sorry my friend. I already knew how to do the code...

But ...

I had problem with changing the src of the embed when it was playing a song.

Just looked at your code. I must have to stop the song before the change of the src can come in effect.

Sorry for wasting your time ... if it doesnt work. I will have a closer inspection of your code. I was just looking for tips. I like to have a go myself.

Thanks alot.

Jon

JavaScript Beginner at work :)
 
This is the code i have.

This is my function in which i change the src of embedded song

Code:
function alter_player(what_to_do) {

	if(what_to_do == "play") {
	document.use_this.play();
	document.getElementById("chosen_action").innerHTML = "play song";
	}



	if(what_to_do == "stop") {
	document.use_this.stop();
	document.getElementById("chosen_action").innerHTML = "stop song";	
	}



	if(what_to_do == "previous") {
	document.use_this.stop();
	document.getElementById("chosen_action").innerHTML = "previous song";

	if(song_number == 0) {
	song_number = music_choice.length
	document.getElementById("use_this").src = music_choice[song_number--];
        }
	
	else {
	document.getElementById("use_this").src = music_choice[song_number--];
	}
	
	document.use_this.play();
	}


	
	if(what_to_do == "next") {
	document.use_this.stop();
	document.getElementById("chosen_action").innerHTML = "next song";
	
	if(song_number == music_choice.length) {
	song_number = 0;
	document.getElementById("use_this").src = music_choice[song_number++];
	}

	else {
	document.getElementById("use_this").src = music_choice[song_number++];
	}
	
	document.use_this.play();	
	}

}

This is the array for my songs to be held

Code:
var music_choice = new Array();
music_choice[0] = "[URL unfurl="true"]http://www.freewebs.com/foxandhounds/nirvana-comeasyouare.wav"[/URL]
music_choice[1] = "[URL unfurl="true"]http://www.freewebs.com/foxandhounds/nirvana-lithium.wav"[/URL]
music_choice[2] = "[URL unfurl="true"]http://www.freewebs.com/foxandhounds/bonjoviitsmylife.wav"[/URL]

This is my embed code

Code:
<EMBED SRC="[URL unfurl="true"]http://www.freewebs.com/foxandhounds/nirvana-comeasyouare.wav"[/URL] height="100" width="100" hidden="true" AUTOSTART="FALSE" ID="use_this" NAME="use_this" MASTERSOUND>

and here is my image map code which has all the buttons on "PLAY, STOP, PREVIOUS, NEXT"

Code:
		<map name="music_player">
		<area shape="circle" coords="33,95,30" onclick="alter_player('play')">
		<area shape="circle" coords="95,95,30" onclick="alter_player('stop')">
		<area shape="circle" coords="31,163,30" onclick="alter_player('previous')">
		<area shape="circle" coords="96,163,30" onclick="alter_player('next')">
		</map>
I ...

1) Stop the Song (this works)
2) Change the src of the embedded song (this works)
3) Play the new song (this doesnt work)

it just plays the original embedded song ...

Sorry to be a pain, but i would love to find out what the hell i am doing wrong... thanks again.

Jon

JavaScript Beginner at work :)
 
please dont tell me youve all gave up on me!

JavaScript Beginner at work :)
 
You gave up on me, I thought.

If the code I gave was not what you wanted, or didn't have any bits you could re-use, then maybe you could explain slightly better what it is you want?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
your code doesnt work ... as it is the same as the code i have written myself.

Have a look at the post above the one i just wrote.

I can succesfully change the src, as i have checked this by alerting it before i play the song again.

So i am stopping the song...changing the source...but then when i play it again...it plays the original embedded song...???

JavaScript Beginner at work :)
 
Try taking my code above, and using one of these two lines to set the song (after the Stop method call, but before the play):

Code:
myPlayer.SetHREF('[URL unfurl="true"]http://www.freewebs.com/foxandhounds/'[/URL] + music[songNum]);
myPlayer.SetURL('[URL unfurl="true"]http://www.freewebs.com/foxandhounds/'[/URL] + music[songNum]);

I do not know which, if either, will work for you, as I am unable to test from work.

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
None of them work either. The src of the song is being changed but then it just plays the original song. I really appreciate your help on this matter but unfortunately it is not working.

Any more ideas?

Sorry to harass you at work.

JavaScript Beginner at work :)
 
anyone, im totally screwed on this, heres my thinking this should be relatively easy ...

JavaScript Beginner at work :)
 
There are actually methods in which i can delete an embed?

I am abit confused at the moment.

Any tips to send me on my way...


JavaScript Beginner at work :)
 
OK - here you go - and it works, too!

Code:
var music = new Array();
music[0] = 'nirvana-comeasyouare.wav';
music[1] = 'nirvana-lithium.wav';
music[2] = 'bonjoviitsmylife.wav';

var songNum = 0;
function alter_player(what_to_do) {
    var myPlayer = document.getElementById('use_this');

    if(what_to_do == 'play') {
        myPlayer.play();
        document.getElementById('chosen_action').innerHTML = 'Playing song ' + (songNum+1) + ' of ' + music.length;
    }

    if(what_to_do == 'stop') {
        myPlayer.stop();
        document.getElementById('chosen_action').innerHTML = 'Stop';
    }

    if(what_to_do == 'next') {
        myPlayer.stop();
        songNum++;
        if (songNum == music.length) songNum = 0;    // If we've reached past the last song, loop around to the first
		myPlayer = reinitialise_player('[URL unfurl="true"]http://www.freewebs.com/foxandhounds/'[/URL] + music[songNum]);
        document.getElementById('chosen_action').innerHTML = 'Playing song ' + (songNum+1) + ' of ' + music.length;
    }

    if(what_to_do == 'previous') {
        myPlayer.stop();
        songNum--;
        if (songNum == -1) songNum = music.length-1;    // If we've reached past the first song, loop around to the last
		myPlayer = reinitialise_player('[URL unfurl="true"]http://www.freewebs.com/foxandhounds/'[/URL] + music[songNum]);
        document.getElementById('chosen_action').innerHTML = 'Playing song ' + (songNum+1) + ' of ' + music.length;
    }
}

function reinitialise_player(url) {
    var myPlayer = document.getElementById('use_this');
	myPlayer.parentNode.removeChild(myPlayer);
	document.getElementsByTagName('body')[0].appendChild(myPlayer = document.createElement('embed'));
	with (myPlayer) {
		setAttribute('src', url);
		setAttribute('height', '100');
		setAttribute('width', '100');
		setAttribute('hidden', 'true');
		setAttribute('autostart', 'true');
		setAttribute('id', 'use_this');
		setAttribute('name', 'use_this');
		setAttribute('mastersound', '');
	}
	return(myPlayer);
}

Not the tidiest way, but hey... that's showbiz!

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Please tell me why i am pissing myself laughing. Do you mind me using LMFAO!!!. hahahahaha! Why am i laughing???

Thanks man, i'll have to learn DOM. any good tutorials you can give me?


Thanks man (again)! I wont just take the piss and use it! i'll learn from it, your a star, and by the way. Your site is awesome. You should be a millionaire by now you brainy little sod. Just the function i need here :p

P.S give me your brain :p

JavaScript Beginner at work :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top