Words from Alejandro U. Alvarez
Programming
All about server and client side back-end programming, php, javascript, mysql…
JW Player Captions
Jun 1st
Captions are a great way to offer multi-lingual media content on your site, but they don't seem easy to add right? Well with the right tools and guidance, it will be very easy:
Setting everything up:
In order for captions to work flawlessly on every major browser, we will be using JW Player version 4.5 (At the time of writing, version 5 and 5.1 don't seem to work well in Internet Explorer)
Once you have that version ready, add the following Flashvars:
<param name="flashvars" value="plugins=captions-1&captions=/captions.xml" />
Of course you may leave all the other Flashvars you had before (Specially file
)
The XML file:
Now that the player is ready, we need to setup the XML file with the captions, so create captions.xml and write the following:
<tt xml:lang="en" xmlns="http://www.w3.org/2006/10/ttaf1" xmlns:tts="http://www.w3.org/2006/10/ttaf1#style"> <body> <div xml:id="captions"> <p begin="00:00:00" end="00:00:4">Subtitle 1</ p> <p begin="00:00:4" end="00:00:6">Subtitle 2</ p> <p begin="00:00:10" end="00:00:12">Subtitle 3</ p> </div> </body> </tt>
As you can see, all we need is to change the Subtitle text, and the begin and end times of each, to set those up.
Generate the XML file with PHP:
If you want to change the language dynamically, you could use PHP to generate the XML using the following headers:
header("Content-type: text/xml"); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past (no cache) // And now all the XML data as before
If you want to learn how to easily translate your website read my article on that too!
Hope you found this useful!
Extract HTML from XML in a jQuery AJAX request
May 25th
This is a bit tricky when you are parsing XML with jQuery. As you will realize, all HTML passed is ignored, and only the text is displayed. I have seen many people do this, which unfortunately won't work!
$(xml).find('element').each(function(){ if($(this).html() < 0){ alert($(this).html()) } });
Now this will throw an error, you can't take HTML from XML attributes like that!, you have to use $(this).text() as your function. So how will we do this?
The solution is very simple, where you generate the XML, make sure you wrap the html code with the CDATA marker as follows:
<element attribute="value"><![CDATA[Some HTML <span style="color:red">code</span> here]]></element>
And that will work perfectly!
iPhone web development [Part 2]
May 21st
In part one of my tutorial on iPhone web development, we talked about the basic stuff (Recognizing iPhones or iPods, setting up special HTML and CSS tags... ) we will now move on to the really clever bit: Orientation detection.
For many sites, we may just have the same thing both on portrait and landscape. But sometimes we will want to use that extra space to display our content differently, and here is how.
Orientation detection with CSS:
We can use some smart CSS3 selectors to ensure that some styles only apply to landscape, or to portrait modes:
/* Portrait */ @media screen and (max-width: 320px){ h1{ font-size:20px; } } /* Landscape */ @media screen and (min-width: 321px){ h1{ font-size:24px; } }
Remember that for this to work we must use the viewport meta tag I showed you in my previous tutorial
This media query works (According to Apple's documentation) since iPhone OS 1.0, so there shouldn't be any problem with it, Firefox mobile should also support this, so by using max-device-width and min-width we should be able to develop specific CSS for every device without the need of PHP to include only certain style sheets
Orientation detection with JavaScript:
We can easily detect the device orientation by checking the innerWidth. This method will also work for other PDAs and SmartPhones:
var updateLayout = function() { if (window.innerWidth != currentWidth) { currentWidth = window.innerWidth; var orient = (currentWidth == 320) ? "profile" : "landscape"; document.body.setAttribute("orient", orient); window.scrollTo(0, 1); } }; iPhone.DomLoad(updateLayout); setInterval(updateLayout, 500);
This code will add a new attribute (orient) to the body element, which will be either portrait or landscape. All we have to do now is use CSS to filter out whatever orientation we need:
There is another method, which will work only on the iPhone though:
body[orient="landscape"]{ // CSS code here... } // ... or ... body[orient="portrait"]{ // CSS code here... }
That JavaScript also hides the toolbar by using the scrollTo() function when the orientation changes,
Next release:
On my next release in this series of iPhone web development tutorials, I will get into the really advanced JavaScript API we have for the iPhone (touch detection, gestures... etc) so stay tuned!
Hope you found this useful,
Alex
PHP: Display all files/pictures in a folder
May 4th
If we want to create a quick gallery of files/pictures, it is quite a pain to do so manually. And since this is some code I'm always reusing I thought I might share it here with everyone else:
Let's suppose we are in the base directory (www.mysite.com/gallery.php) and the pictures are in a folder named pictures (www.mysite.com/pictures/), open gallery.php and where you want the pictures to appear, use this code:
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/'); while($file = readdir($handle)){ if($file !== '.' && $file !== '..'){ echo '<img src="pictures/'.$file.'" border="0" />'; } }
This code is really easy to understand, we first define a variable "$handle", which will contain the handle to the absolute path of the folder (I always go for absolute paths since I find them much safer)
dirname(realpath(__FILE__)) returns the absolute path to the current file (gallery.php) and then we add to the end of it /pictures/
Then, we loop through all the files in the directory, that simple if statement I used is to prevent some the function returning . or .. as file names sometimes. We then display the image in standard html
Take care!
Alex
Play a sound via JavaScript
May 3rd
Working in a project recently I found myself in the need of playing a sort of "alert" sound with JavaScript. It turns out there are not so many good options out there. The first thing that came to my mind was to use an embed and the Play and Stop JavaScript controls.
Well that only works in IE and some versions of Netscape so let's just forget about all those tags (embed, bgsound... etc)
So in my search for an easy solution I came across a very neat jQuery plugin that does exactly that job, the jQuery Sound Plugin by Jorn Zaefferer. I have been completely unable to find the plugin Website, so I will post it's code myself here. This plugin is a port from scriptaculous' version by Jules Gravinese.
How to use:
Using this to play a sound couldn't be easier, let's see an example with no parameters except for the file we want to play:
$.sound.play('files/sample.mp3')
This way the mp3 file sample will start playing. If we prefer the sound to start playing stopping the previous one, we can use tracks, this way we can play one sound per track, and every time we set a new sound for a certain track it stops the current sound there:
$.sound.play('files/sample.mp3',{ track: "track1" });
Other options are:
// timeout: Specify for how long the sound can play in milliseconds $.sound.play('files/sample.mp3', { timeout: 4000 });
We can store a sound reference in an object so that we can later on stop it:
var sound = $.sound.play('files/sample.mp3');
sound.remove();
And finally we have the option to enable/disable sounds at all:
$.sound.enabled = false; $.sound.enabled = true
The plugin code:
(function($) { $.sound = { tracks: {}, enabled: true, template: function(src) { return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>'; }, play: function(url, options){ if (!this.enabled) return; var settings = $.extend({ url: url, timeout: 2000 }, options); if(settings.track){ if (this.tracks[settings.track]) { var current = this.tracks[settings.track]; current.Stop && current.Stop(); current.remove(); } } var element = $.browser.msie ? $('<bgsound/>').attr({ src: settings.url, loop: 1, autostart: true }) : $(this.template(settings.url)); element.appendTo("body"); if (settings.track) { this.tracks[settings.track] = element; } setTimeout(function() { element.remove(); }, options.timeout) return element; } }; })(jQuery);
I will probably minify this code before I upload it to the production server, so if I don't forget I will also post here the minified version,
Hope you found it useful,
Alex
