2

I am trying to figure out how to display an image inside of a div that corresponds to the song that is playing in the playlist. Right now I only have the image showing IF the person has actually CLICKED a song, but I would like it to show the image when the code shifts from one song to the next automatically. I do not know how to do that as I am very new to jquery here are the relevant snippets.

This is just a snippet of the playlist:

<li>
  <a href="audio/02.mp3"onclick="changeImg('audio/2.jpg');" >
    Above All - Michael W. Smith
  </a>
</li>

and this is these are the scripts used(which by the way, I am sure can be condensed in a very big way - but I have not attempted this as of yet!

<script>
$(document).ready(function () {
  init();
  function init(){
    var current = 0;
    var audio = $('#audio');
    var playlist = $('#playlist');
    var tracks = playlist.find('li a');
    var len = tracks.length - 1;
    var imghol = document.getElementById("imageHolder");
    audio[0].volume = .10;
    audio[0].play();
    playlist.on('click','a', function(e){
      e.preventDefault();
      link = $(this);
      current = link.parent().index();
      run(link, audio[0]);
    });
    audio[0].addEventListener('ended',function(e){
      current++;
      if(current == len){
        current = 0;
        link = playlist.find('a')[0];
      }else{
        link = playlist.find('a')[current];    
      }
      run($(link),audio[0]);
    });
  }
  function run(link, player){
    player.src = link.attr('href');
    par = link.parent();
    par.addClass('active').siblings().removeClass('active');
    player.load();
    player.play();
  }
});
</script>

<script type="text/javascript">
  function changeImg(image){
  var imghol = document.getElementById("imageHolder");
  imghol.src = image;
}
</script>    

So far everything works GREAT, but as I said - I would LIKE to have the image that corresponds to the song automatically switch to another image when the song auto plays a different song.

The image showing method was taken from this site where someone else asked how to show an image when clicking on a list item, and I adapted the answer to fit what I needed it to show - but cannot figure out to get it to auto show!

Thanks :)

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • okay, I have found a few places that need to be redone and cleaned up - for starters, the image is not going to show as long as the click function is calling the image change. I think I really need to sit back and try a bit harder to understand the existing code before trying to continue - I want to thank everyone that looked at the question! I will be back if after I do some housecleaning, I am still having problems - good night everyone :) – Rayne Lynch Dec 06 '17 at 01:33

1 Answers1

0

The code below is fully untested. See the edit below.
I re-ordered things in there.

Please change your markup from <a href="audio/02.mp3"onclick="changeImg('audio/2.jpg');" >
to this: <a href="audio/02.mp3">

Adjust your filenames to match!
audio/02.mp3 should match audio/02.jpg
Just the file extention needs to be different.

Then, consider this:

<script>
$(document).ready(function () {
  var current = 0;
  var audio = $('#audio');
  var playlist = $('#playlist');
  var tracks = playlist.find('li a');
  var len = tracks.length - 1;
  var imghol = $("#imageHolder");

  function init(){
    audio[0].volume = .10;
    //audio[0].play();
  }
  init();

  // Your click handler
  playlist.on('click','a', function(e){
    e.preventDefault();
    link = $(this);
    current = link.parent().index();
    run(link, audio[0]);
  });

  // Audio end event handler
  audio.on('ended',function(e){
    current++;
    if(current == len){
      current = 0;
      link = playlist.find('a')[0];
    }else{
      link = playlist.find('a')[current];    
    }
    run($(link),audio[0]);
  });

  // Run function
  function run(link, player){
    player.src = link.attr('href');
    par = link.parent();
    par.addClass('active').siblings().removeClass('active');
    player.load();
    player.play();

    // The image change is here
    href = link.attr("href");
    href = href.replace(".mp3",".jpg");   // changes .mp3 for .jpg ADJUST your file naming
    //imghol.src = href;
    imghol.attr("src",href);
  }

}); // ready
</script>


EDIT

I tryed your code from the PasteBin.

There is just 4 errors to be fixed. The first was from my suggested code.

  1. In the run() function, imghol.src = href; should be imghol.attr("src",href); since imghol is a jQuery object.

The other errors are yours.

  1. href = href.replace("audio/02.mp3","audio/02.jpg"); This line is intended to catch the file name from the href but with a changed extension (from .mp3 to .jpg). My suggestion was correct. It has to be href = href.replace(".mp3",".jpg");
  2. Change <body onload="PlayerLoaded()"> to <body>. This function does not exist anymore.
  3. Remove audio[0].play(); from the init() function. You don't want to play anything yet... There is no file to play!

Now some advises:

Don't use jQuery slim! For the code you actually have, it's ok. But as soon as you will try to add an animation (like a fadeIn()) to the images... It won't work. The slim version is the same as the full version MINUS the animations, Ajax and more usefull stuff. That is only good were performance is important. But without all that jQuery has to offer... Better just use plain JavaScript! If you experiment jQuery, use the full library.

And use the console. All hints to debug your script were there for you to see.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • ahhhhhhh....I just posted that I seen some housecleaning that needs to be done, and then I saw your answer! boy boy boy! I want to thank you for your help and I will give it a try :) – Rayne Lynch Dec 06 '17 at 01:36
  • Louys - I am sorry, I was not clear enough about what I was wanting. Let me try again. I have an mp3 player and have created a playlist using an unordered list. What I am wanting it to do is this: when the person clicks on the song in the list, I want it to play the song and show an image in a div that sits right next to the div with the player in it, it is an image of the lyrics to the song that they have clicked on. The click function works great..they click and the song plays and the image associated with that song displays. – Rayne Lynch Dec 06 '17 at 12:51
  • But I also have this player setup to automatically play the next song in the list, that is where my uneducated problem comes to light! when it auto plays the next song in the list, I was also wanting the image to show that corresponds with the song that is auto playing. – Rayne Lynch Dec 06 '17 at 12:57
  • I understood that from start. That is why I placed the image load in the "run" function... So at the same place as the audio. Have you tried the code? – Louys Patrice Bessette Dec 06 '17 at 14:16
  • yes i did but it would not show any images. In the run code that you used i put("audio/01.mp3, audio/01.jpg") and I changed the unordered list by removing the onclick method and only having the – Rayne Lynch Dec 06 '17 at 19:56
  • I am sure that it is something that I am completely not understanding *sigh* I have only been web building for less than a year, and have used jquery even less time than that. LOL...silly me, I thought that building a simple playlist that shows an image would be a snap...haaaaaa! I am one silly gurl! LOL – Rayne Lynch Dec 06 '17 at 19:58
  • Okay... Just post your whole actual code into a [PasteBin](https://pastebin.com/), I will have a look. – Louys Patrice Bessette Dec 07 '17 at 02:50
  • okay - I am new to pastebin, lol - never heard of it until just now! so..with that being said, I posted my whole code at that pastebin, under username of raynelynch and the title is "code for Louys" - and I want to thank you again for taking the time to try to help me :) – Rayne Lynch Dec 07 '17 at 13:25
  • Sorry about the delay to answer back... You have to post the PasteBin link here. I could not find it with just the title. – Louys Patrice Bessette Dec 08 '17 at 17:17
  • Hi, and yes, now I have to say sorry tooo for my disappearing act as well! here is the link at pastebin https://pastebin.com/Nui9F7mW – Rayne Lynch Dec 11 '17 at 16:15
  • No problem... I edited the answer, based on your code. – Louys Patrice Bessette Dec 12 '17 at 06:49
  • awww thank you so much Louys! I will look over it all and give it a try :)..awesome! – Rayne Lynch Dec 14 '17 at 00:08
  • 1
    GREAT answer Louys! :) - I have it working wonderfully on the test server and it is nice and smooth, awesome! I did mark your edited post as +1 but it tells me I am under 15 points ?! oO I need POINTS!!! anyway, thanks again - I am looking and taking your other advice too, take the preload out of the body tag, ect,, :) – Rayne Lynch Dec 15 '17 at 18:00
  • 1
    yes, since your post - I have been looking at console and it is helping a great deal! albeit it takes some bit of getting use to – Rayne Lynch Dec 15 '17 at 18:02
  • Accepting your first answer (look for the green checkmark) will give you 2 points. And I upvoted your question ;) – Louys Patrice Bessette Dec 15 '17 at 18:21