0

First and foremost I'd like to thank everyone who participates contributes and adds to the forums / community, you guys provide an extremely appreciated and valued resource to those that just need information and assistance when needed. Simple thing to do for some, but to many it means volumes. So thanks again.

I'm no coding or script expert but I definitely learn quick and know enough to get around and translate (especially if the language is explained) attempting to get a player on my site to populate and propagate a playlist based on an uploaded file string.

So far I have the player completely up and running, it properly parses the upload links to display the separate mp3 files to play as well as the separate description information per file.

The problem is when I click the play button all of the files mp3 play at once. I'm almost positive I need to add a forEach statement or something along those lines either here:

$(function(){
    $('#player1').plp({
        'volume': 80,
        'playlist':[
            {"title":"{$pic.video_descr }", 
            "author":"{.Me}", 
            "cover":"files/covers/fullfilename", 
            "mfile":"{.siteurl}{.fullfilename}", 
            "duration":"330"},

        ] 
     });
});

for each title, author, cover, duration and mfile,

or somewhere around here

function init_track(i){
    cpl = i;
    $this.find('.playlist span').removeClass('active');
    $this.find('.playlist span:eq('+i+')').addClass('active');
    $audio.src = options.playlist[i].mfile;
    $this.find('.title').html(options.playlist[i].title);
    $this.find('.author').html(options.playlist[i].author);
    $this.find('.cover').attr('src', options.playlist[i].cover);
}


for(i=0; i < options.playlist.length; i++){
    $this.find('.playlist').append('<span>'+options.playlist[i].author+' - '+options.playlist[i].title+'</span>');
}
init_track(cpl);

$this.find('.playlist span').click(function(){
    init_track($(this).index());
});

here is the full .js

(function($) {
    jQuery.fn.plp = function(options) {
        var options = $.extend({
            volume: 50,
            playlist: [],
            autostart: false
        }, options);

        var make = function() {
            var $this = $(this);
            var cpl = 0;
            var $audio = new Audio();
            var isrand = false;

            $this.find('.volume').slider({
                animate: true,
                range: 'min',
                value: options.volume,
                min: 0,
                max: 1,
                step: 0.01,
                slide: function(event, ui) {
                    $audio.volume = ui.value;
                }
            });

            $this.find('.long').slider({
                animate: true,
                range: 'min',
                value: 0,
                min: 0,
                max: 60,
                step: 1,
                slide: function(event, ui) {
                    $audio.currentTime = ui.value;
                }
            });

            $audio.addEventListener('canplay', function(_event) {
                if ($audio.duration) {
                    $this.find('.all').html('&nbsp;/&nbsp;' + toMinit($audio.duration));
                    $this.find('.long').slider({
                        'max': $audio.duration
                    });
                } else {
                    this.find('.all').html('&nbsp;/&nbsp;' + toMinit(options.playlist[cpl].duration));
                    $this.find('.long').slider({
                        'max': options.playlist[cpl].duration
                    });
                }

                if (options.autostart) {
                    $audio.play();
                    $this.find('.tlb_stop').addClass('isplay');
                } else {
                    options.autostart = true;
                }
            });

            $audio.addEventListener('ended', function() {
                if (isrand) {
                    var rand = cpl;
                    while (rand == cpl) {
                        rand = Math.floor(Math.random((options.playlist.length)));
                    }

                    init_track(rand);
                } else {
                    if (cpl == options.playlist.length - 1) {
                        cpl = -1;
                    }
                    init_track(cpl + 1);
                }
            });

            $audio.addEventListener('timeupdate', function() {
                $this.find('.long').slider({
                    'value': $audio.currentTime
                });
                $this.find('.current').html(toMinit($audio.currentTime));
            });


            function toMinit(val) {
                val = Number(val);
                var ost = Math.floor(val % 60);
                var tm = Math.floor(val / 60);
                if (ost < 10) {
                    ost = '0' + ost;
                }
                if (tm < 10) {
                    tm = '0' + tm;
                }
                return tm + ':' + ost;
            }

            function init_track(i) {
                cpl = i;
                $this.find('.playlist span').removeClass('active');
                $this.find('.playlist span:eq(' + i + ')').addClass('active');
                $audio.src = options.playlist[i].mfile;
                $this.find('.title').html(options.playlist[i].title);
                $this.find('.author').html(options.playlist[i].author);
                $this.find('.cover').attr('src', options.playlist[i].cover);
            }

            for (i = 0; i < options.playlist.length; i++) {
                $this.find('.playlist').append('<span>' + options.playlist[i].author + '  - ' + options.playlist[i].title + ' < /span>');
            }
            init_track(cpl);

            $this.find('.playlist span').click(function() {
                init_track($(this).index());
            });

            $this.find('.tlb_prev').click(function() {
                if (isrand) {
                    var rand = cpl;
                    while (rand == cpl) {
                        rand = Math.floor(Math.random() *

                            (options.playlist.length));
                    }

                    init_track(rand);
                } else {
                    if (cpl === 0) {
                        cpl = options.playlist.length;
                    }
                    init_track(cpl - 1);
                }
                return false;
            });

            $this.find('.tlb_stop').click(function() {
                if ($audio.paused) {
                    $audio.play();
                    $(this).addClass('isplay');
                } else {
                    $audio.pause();
                    $(this).removeClass('isplay');
                }
                return false;
            });

            $this.find('.tlb_next').click(function() {
                if (isrand) {
                    var rand = cpl;
                    while (rand == cpl) {
                        rand = Math.floor(Math.random() *
                            (options.playlist.length));
                    }

                    init_track(rand);
                } else {
                    if (cpl == options.playlist.length - 1) {
                        cpl = -1;
                    }
                    init_track(cpl + 1);
                }
                return false;
            });

            $this.find('.vol_icon').click(function() {
                $(this).toggleClass('active');
                $this.find('.volume').fadeToggle(100);
                return false;
            });

            $this.find('.pl_icon').click(function() {
                $(this).toggleClass('active');
                $this.find('.playlist').fadeToggle(100);
                return false;
            });

            $this.find('.while_icon').click(function() {
                if ($audio.loop) {
                    $(this).removeClass('active');
                    $audio.loop = false;
                } else {
                    $(this).addClass('active');
                    $audio.loop = true;
                }
                return false;
            });

            $this.find('.rand').click(function() {
                if (isrand) {
                    $(this).removeClass('active');
                    isrand = false;
                } else {
                    $(this).addClass('active');
                    isrand = true;
                }
                return false;
            });
        };
        return this.each(make);
    };
})(jQuery);

Here is the PHP/HTML (smarty template):

<script type="text/javascript" src="/jquery.js">   
</script>
<script type="text/javascript" src="/player.js">  
</script>

{foreach item=pic from=$pics} {/foreach}
<table>
    <tr>
        <td valign="middle">
            {if $pic.ext == 'yt'}
            <div style=
            "background-image:url('http://img.youtube.com/vi/{$pic.ytref}/0.jpg');">
            <div style="height:120px; max-width:120px;">
                    <a alt="video no {$pic.videono}" href=
                    "http://www.youtube.com/v/{$pic.ytref}&amp;fs=1&amp;rel=0::"
                    title="Click to Play"><img height="120" onmouseout=
                    "this.src='playbtred.png'" onmouseover=
                    "this.src='playbty.png'" src="playbtred.png" width=
                    "120"></a>
                </div>
            </div>
            <div style=
            "width:60%;word-wrap: break-word;padding-bottom:20px;">
                {$pic.video_descr }
            </div>{elseif $pic.ext == 'mp3'} 
            <script type="text/javascript">


            $(function(){
            $('#player1').plp({
            'volume': 80,
            'playlist':[
                {"title":"{$pic.video_descr }", 
                "author":"Test2", 
                "cover":"files/covers/1.jpg", 
                "mfile":"{$config.siteurl}{$pic.fullfilename}", 
                "duration":"330"},

            ] 
            });


            });   

            </script><br>
            <div style=
            "width:60%;word-wrap: break-word;padding-bottom:20px;">
            </div>{else} {include file='otheruploads.tpl'}<br>
            {$pic.video_descr } {/if}
        </td>
    </tr>
</table>
<div class="player" id="player1">
    <img alt="" class="cover" src=""><!--album cover-->
     <span class="title"></span><!--song title-->
     <span class="author"></span><!--artist-->
    <div class="long"></div>
    <div class="current">
        00:00
    </div>
    <div class="all"></div>
    <div class="volume"></div>
    <div class="vol_icon"></div>
    <div class="rand"></div>
    <div class="while_icon"></div>
    <div class="toolbar">
        <div class="tlb_prev"></div>
        <div class="tlb_stop"></div>
        <div class="tlb_next"></div>
    </div>
    <div class="pl_icon active"></div>
    <div class="playlist flexcroll"></div>
</div>{/if}

I'm just not sure where or how. I've literally spent the last two days searching and researching to figure it out. I'm so sure its simple, but I'm lost at the moment, and would GREATLY appreciate the help.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    This looks like you use some kind of templating language to create JavaScript for you. Apart from the fact that this might not be the smartest thing to do in general, for the question at hand it helps to show the JavaScript as the browser sees it. – Tomalak Jan 27 '16 at 15:46
  • 1
    @Teemu sorry i forgot to remove the delimiters – Atlcustoms Atlcustoms Jan 27 '16 at 16:32
  • @Tomalak sorry, I added the js and the PHP/HTML. you are correct, I am using smarty for this project, the player will populate based off a defined name and location and call everything. it functions. I can supply you with a link to the page so you can see exactly what the script outputs – Atlcustoms Atlcustoms Jan 27 '16 at 17:32
  • I think what you really should do first (instead of fixing your current approach) is to make your JS static and to separate it into an external file. Creating JavaScript dynamically is asking for trouble. If you want to transport data - like a playlist - to to the client, do so by using the JSON data format (see [this question](http://stackoverflow.com/a/1426736/18771)) - for example in a `data-playlist` HTML attribute which you subsequently access from your static JS. – Tomalak Jan 27 '16 at 18:07
  • Thank you @Tomalak I was actually looking at using JSON for a few hours and played a bit with it. My .js files are external from the smarty engine and database, so I'm going to have to figure how to get the external .js to still connect to / translate the URLs in the smarty database. – Atlcustoms Atlcustoms Jan 27 '16 at 18:39

0 Answers0