• I’m trying to use the CSS & Javascript Toolbox plugin to add javascript to my main page that plays audio, but it isn’t working. Here is the code I am using:

    <audio id="audio" preload="auto" tabindex="0" controls="" type="audio/ogg" style="display: none;">
    </audio>
    <div id="playing" style="display: none;">
    </div>
    <script>
    var audio;
    var playlist;
    var tracks;
    var current;
    var musicarr = ["https://domain/siteaudio.ogg",
                    "https://domain/siteaudio2.ogg",
                    "https://domain/siteaudio3.ogg",
                    "https://domain/siteaudio4.ogg",
                    "https://domain/siteaudio5.ogg",
                    "https://domain/siteaudio6.ogg",
                    "https://domain/images/siteaudio7.ogg",
                    "https://domain/siteaudio8.ogg",
                    "https://domain/siteaudio9.ogg",
                    "https://domain/siteaudio10.ogg",
                    "https://domain/siteaudio11.ogg",
                    "https://domain/siteaudio12.ogg",
                    "https://domain/siteaudio13.ogg",
                    "https://domain/siteaudio14.ogg",];
    shuffle(musicarr);
    
    init();
    function init(){
        current = 0;
        audio = $('audio');
        audio[0].volume = .20;
        len = musicarr.length;
    
        run(musicarr[current], audio[0]);
    
    }
    
    function run(link, player){
            player.src = link;
            audio[0].load();
            audio[0].play();
            $('#playing').html("<ul><li><a>" + link+ "</a></li></ul>");
    }
    
    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex ;
    
      // While there remain elements to shuffle...
      while (0 !== currentIndex) {
    
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
    
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }
    
      return array;
    }
    </script>

    I have code for MP3 as well. That is just ogg. Can someone tell me how to use this code? TIA!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You really need to link the Webpage with the issue

    Thread Starter The One

    (@the-one-1)

    I don’t see how giving my blog link matters. I just need help with the code. But anyway the blog link is blog.systechforum.net

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    When you describe your error as “it isn’t working”, that doesn’t give us much to go on. If you’re not familiar with the code and cannot identify what exactly is wrong then just throw us the link to the page with the issue and let us explore it (and thank you for doing that).

    It doesn’t look like the CSS & Javascript Toolbox plugin is active on your site. Check the source code of your page and search for “css-javascript-toolbox” (the name of the plugin).
    view-source:https://blog.systechforum.net/

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    When it becomes active, you need to make sure you’re only pasting JavaScript into the JavaScript section of that plugin. The code above contains HTML as well as JavaScript. That won’t work. The HTML should go in the Text editor of the page you want it to appear on.

    Thread Starter The One

    (@the-one-1)

    Yes, I did a search (Control + F) in the source code and did not find CSS & Javascript Toolbox, yet it is installed as I can add code in the plugin in the dashboard. I will add the html section under HTML and then the scrip under Java script and see what happens.

    Thread Starter The One

    (@the-one-1)

    Okay, nothing I try in the CSS & Javascript Toolbox plugin is working. By that I mean the audio won’t play. I added the code to the HTML input this time and there were no errors displayed, I save and activate and no audio is in the site. If the plugin isn’t active how do I activate it? When I installed it had to add database entries and other stuff. Not sure if I missed a step. Like I said, I can access the plugin and enter code, but the audio will not play.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    It seems like there’s a problem with the plugin. For support on that I would recommend you post here: https://www.ads-software.com/support/plugin/css-javascript-toolbox#postform

    I did take out the JavaScript, above, and try to run it on your page. There are a few things wrong with the code itself. I’ve resolved those problems with the code below, but ultimately it needs to be tested with the right HTML in place too.

    var $ = jQuery,
    	audio,
    	playlist,
    	tracks,
    	current,
    	musicarr =
    		["https://domain/siteaudio.ogg",
    		"https://domain/siteaudio2.ogg",
    		"https://domain/siteaudio3.ogg",
    		"https://domain/siteaudio4.ogg",
    		"https://domain/siteaudio5.ogg",
    		"https://domain/siteaudio6.ogg",
    		"https://domain/images/siteaudio7.ogg",
    		"https://domain/siteaudio8.ogg",
    		"https://domain/siteaudio9.ogg",
    		"https://domain/siteaudio10.ogg",
    		"https://domain/siteaudio11.ogg",
    		"https://domain/siteaudio12.ogg",
    		"https://domain/siteaudio13.ogg",
    		"https://domain/siteaudio14.ogg"];
    
    $(document).ready(function() {
        shuffle(musicarr);
        init();
    });
    
    function init() {
        current = 0;
        audio = $('audio');
        audio[0].volume = .20;
        len = musicarr.length;
    
        run(musicarr[current], audio[0]);
    }
    
    function run(link, player){
    	player.src = link;
    	audio[0].load();
    	audio[0].play();
    	$('#playing').html("<ul><li><a>" + link+ "</a></li></ul>");
    }
    
    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex ;
    
      // While there remain elements to shuffle...
      while (0 !== currentIndex) {
    
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
    
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }
    
      return array;
    }

    [Code updated]

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘jquery and audio embedding’ is closed to new replies.