Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    basically, but using the src attribute directly, not innerHTML. Something like:
    Code:
    var player=document.getElementById("audplayer");
    player.stop() // or pause() - I don't remember which
    player.src=tracks[tracknumber];
    player.play();

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    NOT innerHTML. Try to avoid innerHTML if at all possible. Change the attributes of an object, instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    New Coder
    Join Date
    Jul 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    so I decided to go a different route and added another button instead to play a random song. The code belows works fine. However, when the random track finishes playing, and the play button is clicked again, same track plays. It only randomizes after a page is refreshed.

    What I am trying to do is, play a random track each time the button is clicked without the need to refresh the page. Any ideas?

    javascript:
    Code:
                var tracks = [ "audiotag1","audiotag2","audiotag3"];
          
          var randomTrack = tracks[Math.floor(Math.random()*tracks.length)];
    		
    	function playTrackR(){
          document.getElementById(randomTrack).play();
    	  document.getElementById('playR').style.display = "none";
    	  document.getElementById('pauseR').style.display = "block";
    	  }
    
    	function pauseTrackR() {
          document.getElementById(randomTrack).pause();
    	  document.getElementById('pauseR').style.display = "none";
    	  document.getElementById('playR').style.display = "block";
    	  }
    HTML
    Code:
    	<a href="javascript:playTrackR();">
    	   <img id="playR" src="img/playR.gif">
    	</a>
    																		
    	<a href="javascript:pauseTrackR();">
    	   <img id="pauseR" src="img/pauseR.gif">
    	</a>

  4. #19
    New Coder
    Join Date
    Jul 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    solved it, had to add this
    Code:
    var randomTrack = tracks[Math.floor(Math.random()*tracks.length)];
    to playTrackR function

  5. #20
    New Coder
    Join Date
    Jul 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I got happy too early. The pause() function no longer works after that


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •