Archive

Archive for March 28, 2010

Javascript Optimization Tip: Faux-StringBuilder

March 28, 2010 Leave a comment

In my new site, Aud (which you can see my original post on here), I use a lot of javascript, and I wanted it to be very fast. One part that was amazingly slow was the generation of the playlist. The script does an ajax call to the server to get the playlist as JSON, and then turns each song into a table row and appends it to the table in the playlist div. Simple, yes, but if you do it wrong and have a lot of data, it’s very slow. Heres a tip I figured out to help speed up the process:

Instead of using your typical concatenation techniques like this:

var table = "";
for (var i = 0; i < playlist.length; i++) {
   table += "<tr><td><strong>Artist: " + artist + " Album: " + album + " Song: " + song + "</td></tr>";
}
$('#table').append(table);


You would do something like this:


var table = [];
for (var i = 0; i < playlist.length; i++) {
   table.push(["<tr><td><strong>Artist: ", artist, " Album: ", album, " Song: ", song, "</td></tr>"].join(''));
}
$('#table').append(table.join(''));


Now some people might think, “Well, why not put the .append() inside the loop?”. You really don’t want to do that. It’s very costly in terms of speed, and ends up even slower.

So if you have a lot of appending to strings, try making it an array, and pusing and joining!

Categories: JavaScript Tags: ,

Introducing Aud: A Simple HTML 5 Media Player

March 28, 2010 Leave a comment

On my main desktop, for whatever reason, I couldn’t get the sound to work in any program except my browser. So for a while I was stuck listening to Pandora and YouTube videos (don’t get me wrong, I like Pandora, but after a while I just wanted to listen to the music in my own library).  And then, one day, I was reading an article about HTML 5, and I realized that maybe I could put some of the new elements to good use and make myself a way to listen to my music. And that’s exactly what I did.

With HTML 5′s new audio tag, some JQuery, and about 150 lines of PHP, I produced this (Warning: IE and FireFox not supported. Chrome and Safari only). The whole thing is about 300 lines of JS, but that is far more than is needed to make yourself a simple little audio player. In this post I’ll be covering the basics of how it works, and I will be making other posts that go into details about other parts of the app.



<!DOCTYPE html>
<html>
   <head>
      <title>Aud</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="func.js" type="text/javascript"></script>
<script type="text/javascript">
         playlist = [
            {
               artist: "10 Years",
               album: "The Autumn Effect",
               file: "Waking Up",
               url: "Waking_Up.mp3"
            },
            {
               artist: "10 Years",
               album: "The Autumn Effect",
               file: "Fault Line",
               url: "Fault_Line.mp3"
            },
            {
               artist: "10 Years",
               album: "The Autumn Effect",
               file: "The Recipe",
               url: "The_Recipe.mp3"
            }
         ]; // 3 song playlist

         $(document).ready(function() {
            aud = $('#player .aud').get(0);
            aud.pos = 0;

            $('#player .play').bind('click', function(evt) {
               evt.preventDefault();
               if (aud.networkState == 0) {
                aud.pos = -1;
                  $('#player .next').trigger('click');
               } else {
                  aud.play();
               }
            });

            $('#player .pause').bind('click', function(evt) {
               evt.preventDefault();
               aud.pause();
            });

            $('#player .next').bind('click', function(evt) {
               evt.preventDefault();
               aud.pause();
               aud.pos++;
               if (aud.pos == playlist.length) aud.pos = 0;
               aud.setAttribute('src', playlist[aud.pos].url);
               $('#songname').html("

<strong>Artist:</strong> " + playlist[aud.pos].artist + "
<strong>Album:</strong> " + playlist[aud.pos].album + "
<strong>File:</strong> " + playlist[aud.pos].file + "

");
               aud.load();
               aud.play();
            });

            $('#player .prev').bind('click', function(evt) {
               evt.preventDefault();
               aud.pause();
               aud.pos--;
               if (aud.pos < 0) aud.pos = playlist.length - 1;
               aud.setAttribute('src', playlist[aud.pos].url);
               $('#songname').html("

<strong>Artist:</strong> " + playlist[aud.pos].artist + "
<strong>Album:</strong> " + playlist[aud.pos].album + "
<strong>File:</strong> " + playlist[aud.pos].file + "

");
               aud.load();
            });

            aud.addEventListener('canplay', function(evt) {
               $('#player .play').trigger('click');
            });

            aud.addEventListener('ended', function(evt) {
               $('#player .next').trigger('click');
            });
         });
</script>
</head>
   <body>
      <div id='main'>
         <div id='player'>
            <div id='controls'>
               <div id='back' class='prev'></div><div id='play' class='play'></div><div class='next' id='forward'></div>
               <a href='#' class='pause'>Pause</a>
               <div id='songname'>
                  <p>Song</p>
               </div>
            </div>
            <audio class='aud'>
               <p>Oops, looks like your browser doesnt support HTML 5!</p>
            </audio>
         </div>
      </div>
   </body>
</html>

This is all you need to make yourself a functioning little player. It’s pretty simple. I’ve quite enjoyed working its really easy API.

FYI, the reason my app is not supported in Firefox is because its audio tag doesn’t support the mp3 format like Chrome and Safari do. Right now firefox only supports the .ogg format.

There is a lot more that went into this app that I will be posting on later, so be sure to check back!

Categories: Technology Tags: , , ,
Follow

Get every new post delivered to your Inbox.