Search code examples
javascriptphpjqueryopencartaudiojs

Error loading: "undefined" when using audio.js with Opencart to show audio playlist


im trying to integrate a mp3 module into my opencart store using audio.js from http://kolber.github.io/audiojs/demos/test6.html. so i followed the steps mentioned in http://forum.opencart.com/viewtopic.php?t=80116&p=372808 the code was buggy so i fixed it.

But the problem is when i load the page i get the below error

Error loading: "undefined"

enter image description here

when i click on a song in the playlist and click on the player it plays the song. but it still says undefined.

it actually should be like enter image description here

below is the code header.tpl

 <script src="/js/audiojs/audio.min.js"></script>
       <script>
              $(function() {
                // Setup the player to autoplay the next track
                var a = audiojs.createAll({
                  trackEnded: function() {
                    var next = $('ol li.playing').next();
                    if (!next.length) next = $('ol li').first();
                    next.addClass('playing').siblings().removeClass('playing');
                    audio.load($('a', next).attr('data-src'));
                    audio.play();
                  }
                });

            // Load in the first track
            var audio = a[0];
                first = $('ol a').attr('data-src');
            $('ol li').first().addClass('playing');
            audio.load(first);

            // Load in a track on click
            $('ol li').click(function(e) {
              e.preventDefault();
              $(this).addClass('playing').siblings().removeClass('playing');
              audio.load($('a', this).attr('data-src'));
              audio.play();
            });
            // Keyboard shortcuts
            $(document).keydown(function(e) {
              var unicode = e.charCode ? e.charCode : e.keyCode;
                 // right arrow
              if (unicode == 39) {
                var next = $('li.playing').next();
                if (!next.length) next = $('ol li').first();
                next.click();
                // back arrow
              } else if (unicode == 37) {
                var prev = $('li.playing').prev();
                if (!prev.length) prev = $('ol li').last();
                prev.click();
                // spacebar
              } else if (unicode == 32) {
                audio.playPause();
              }
            })
          });
        </script>

in product.tpl

  <?php // Start of player ?>
        <?php if ($attribute_groups) { ?>
 <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
          <?php if ($attribute_group['name'] == 'Demo Tracks') { ?>

            <div class="player">
              <audio preload></audio>
                <ol>
                  <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
                    <?php //split name
                       $track = strtok($attribute['text'],"~");
                    ?>
                    <?php while ($track !== false) {
                      // probably a better way, but put the 2 into diff vars
                      $track_url = $track;
                      $track = strtok('~');
                      $track_desc = $track;
                      $track = strtok('~');
                    ?>.
                    <li>
                      <a href="#" data-src="<?php echo html_entity_decode($track_url); ?>"><?php echo $track_desc ?>
                    <?php } // end while ?>
                  <?php  } // end foreach?>
                </ol>
              </div>
            <?php } //if attribute_group ?>
 <?php }  ?>
          <?php } //if attribute ?>

any help will be appreciated.


Solution

  • Looking at your JS code I can see you are using only selector like this

    $('ol li')
    

    which is going to find all the <li> elements inside of <ol> element. While this should normally work fine it is highly probable that in your template you have another <ol><li> that does not contain your playlist entries.

    A quick fix for this could be achieved by using a class or id attribute for your playlist <ol>, so it looks like e.g. <ol id="playlist">. Then the unique selector would be

    $('ol#playlist li')