» tagged pages
» logout

sorted by: recent | see : popular
Content Tagged with Examples + Library

Reading ID3 tags with JavaScript

Jacob Seidelin is up to more tricks, this time playing with the binary side of life and writing a library that can reading ID3 tags from MP3 files and such.

JAVASCRIPT:
  1.  
  2. // URL of the mp3 file (must be on the same domain!)
  3. var file = "mymusicfile.mp3";
  4.  
  5. // define your own callback function
  6. function mycallback() {
  7.  // either call the ID3.getAllTags([file]) function which returns an object holding all the tags
  8.  alert(
  9.   "All tags in this file: " + ID3.getAllTags(file).toSource()
  10.  );
  11.  
  12.  // or call ID3.getTag([file], [tag]) to get a specific tag
  13.  alert(
  14.   "Title: " + ID3.getTag(file, "title") + " by artist: " + ID3.getTag(file, "artist")
  15.  );
  16. }
  17.  
  18. ID3.loadTags(file, mycallback);
  19.  

You can view a demo at work or download the code.

Of course, Jacob realises that this doesn't make sense for many use cases:

Of course, one big disadvantage of doing this on the client in JavaScript is that the you need to download the entire MP3 file before the tags are available, so it might be better to stick to server-side solutions in many cases if all you need is the tag info.

Ajax: Ajaxian

JSONVid: Pure JavaScript Video Player

Jacob Seidelin went on a ( crazy :) ) mission to create a pure JavaScript video player that didn't use Flash:

My first thought was to read binary video files using a technique like the Andy Na posted about here, figuring that there must be some really simple to parse video formats around, but I soon changed directions and decided to make up a whole new video format. Enter.. JSONVid. Using a player like mplayer, it is easy to export all frames in a movie clip to individual jpeg files, and using whichever language you prefer it is also fairly trivial to collect these files, base64 encode the bunch of them and throw them all together in a nice JSON file (I used this PHP script).

The format uses good ole data: URLs, which are finally supported in IE with version 8:

JSON:

    {
      frm : "JSVID",   // format id tag
      ver : 1,  // version number of format
      width : 320,  // width of video
      height : 240,  // height of video
      rate : 15,  // framerate (frames per second)
      frames : 495,  // number of frames in file
      data : {
        video : [ // here comes 495 data:uris containing base64 encoded jpeg image frames
          "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/7gAOQWRv ... ",
          "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/7gAOQWRv ... ",
          ...
        ]
      }
    }

Then he created the player:

First strategy was to create an Image object for each frame and render it on a canvas element using drawImage(). That worked fine and performance was nice (although Opera used a lot of CPU), but I figured I'd try just using a regular image tag and just change the src property to another data:uri each frame. The change was barely noticeably in Firefox and Safari and it ran a bit better in Opera, so I lost the canvas and stuck with plain old images.

Now, it seems that Firefox will eat up all the memory in the world if you keep throwing new data:uris at the same image tag, which led to another change, so for each frame a new Image object was created and saved for later and as the video played, the previous frame Image was replaced by the new Image object. That seemed to work, but introduced an annoying delay as all these Image objects were created before playing, so I ended up moving the Image creation to actual render cycle where it simply checks if the frame Image has already been created, and if not, creates it.

You can now get going with HTML such as:

HTML:
  1.  
  2. <script src="jsvideo.js" type="text/javascript"></script>
  3. </head>
  4. <div videosrc="myvideo.jsvid" videoautoplay="true"></div>
  5. </body>
  6. </html>
  7.  

There are a couple of tests to play with. It was also pointed out that maybe an animated gif/png would be a choice (without controls), and of course, Flash is still the best choice here, until we get video support in most browsers.

Ajax: Ajaxian

Preventing spam with drag and drop

I just saw a fun little solution to spam prevention that will truly annoy your users. The drop comment solution uses jQuery and its drag and drop support to require that you move your comment over to the drop zone before the comment is saved.

I also recently talked about how Passpack lets you click on a box to prove you are not a bot. How much nicer is that that typing in some letters that you can't read. Imagine it on the iPhone where you can just touch the right icon.

jQuery Comment Spam

Ajax: Ajaxian

$: Now with more magic!

Dustin Diaz is on a roll :) He has posted about Roll out your own JavaScript Interfaces in which he discusses the desire to use style from libraries such as prototype, jquery and friends, yet in a small bit of code where you don't want to use the library:

There are times when using a JavaScript library is called for. Building large web applications that use a wide array of utility functions that help aid in developing multi-tiered class systems, advanced UI components, complex event models, and heavy use of DOM scripting helpers. Yep. Those are all great.
However, there are other times when you don’t need all that. And often what we end up doing is just importing a few of our favorite functions as globals, and work off those. But what ends up happening in this case is that we lose the particular style that these libraries offer. For instance, I’d still like to be able to do something like this without a library.

Dustin would like to write something like this:

JAVASCRIPT:
  1.  
  2. $('foo', 'bar').on('click', function(e) {
  3.     $(this).css({
  4.         color: 'green',
  5.         fontSize: '2em'
  6.     }).addClass('active');
  7. });
  8.  

And to implement the magic he takes Prototypes $() and makes it a special object that can do much more:

JAVASCRIPT:
  1.  
  2. (function() {
  3.   // private constructor
  4.   function _$(els) {
  5.     this.elements = [];
  6.     for (var i=0; i<els .length; i++) {
  7.       var element = els[i];
  8.       if (typeof element == 'string') {
  9.         element = document.getElementById(element);
  10.       }
  11.       this.elements.push(element);
  12.     }
  13.     return this;
  14.   }
  15.  
  16.   _$.prototype = {
  17.     each: function(fn) {
  18.       for ( var i = 0, len = this.elements.length; i<len; ++i ) {
  19.         fn.call(this, this.elements[i]);
  20.       }
  21.       return this;
  22.     },
  23.     setStyle: function(prop, val) {
  24.       this.each(function(el) {
  25.         el.style[prop] = val;
  26.       });
  27.       return this;
  28.     },
  29.     addClass: function(className) {
  30.       this.each(function(el) {
  31.         el.className += ‘ ‘+className;
  32.       });
  33.       return this;
  34.     },
  35.     on: function(type, fn) {
  36.       var listen = function(el) {
  37.         if (window.addEventListener) {
  38.           el.addEventListener(type, fn, false);
  39.         } else if (window.attachEvent) {
  40.           el.attachEvent(’on’+type, function() {
  41.             fn.call(el, window.event);
  42.           });
  43.         }
  44.       };
  45.       this.each(function(el) {
  46.         listen(el);
  47.       });
  48.       return this;
  49.     },
  50.     css: function(o) {
  51.       var that = this;
  52.       this.each(function(el) {
  53.         for (var prop in o) {
  54.           console.log(prop);
  55.           that.setStyle(prop, o[prop]);
  56.         }
  57.       });
  58.       return this;
  59.     }
  60.   };
  61.   window.$ = function() {
  62.     return new _$(arguments);
  63.   }
  64. })();
  65.  

You can see a demonstration at work.

Ajax: Ajaxian

Page 1 | Next >>