Adding Yahoo! Media Player to Your Web Page

June 9, 2009

Note: This post is one of a series of posts that breaks out the individual topics of my presentation “Pimp My Web Page” given at WebVisions 2009. You can see the fully pimped page that includes the code detailed below at http://pimpedblog.harmonicnw.com. Code tested for XHTML 1.0 Strict pages on IE 6 and 7 for PC, Firefox 3 for PC/mac, Safari 4 public beta for mac.

I first saw the Yahoo! Media Player on pampelmoose.com, an excellent music blog run by Portlander Dave Allen. I thought it was a really elegant way to integrate a player into a web site. As it turns out, it is also easy to set up and free!

To use the player, put the following code in <head>:

<script type="text/javascript" src="http://mediaplayer.yahoo.com/js"></script>

The media player will then automatically recognize links to mp3 files in your HTML and add them to the playlist.

The Basics

The Yahoo! Media Player should really be called Yahoo! Music Player as all it does at the moment is play mp3 audio. But it does so very well: the player adds a little play/pause button next to your mp3 links and then adds the song to the player that appears as a tab in the lower left part of the screen. The player itself is a Flash application.

You can associate album art to a song within the player by putting an <img> inside of your anchor link. The current version of the player uses a 46 pixel by 46 pixel image. You can add the style display:none to hide the image on the page. For example:

<a title="Nearly" href="http://pimpend.harmonicnw.com/media/nearly.mp3"><img style="display:none" src="http://pimpend.harmonicnw.com/images/myv1.jpg" width="46" height="46" alt="Metaphorically Yours [vol 1] album cover" />Play 'Nearly'</a>

By default, the player displays the same text as you have in your HTML as the title of the song. If you want to make the title something different, you can add a title value in your anchor tag. In the above example, the title will appear as “Nearly.”

For more information on using the Yahoo! Media Player, go check out the documentation. They explain everything there as/more clearly than I can.

What if I Don’t Want Users to Download My MP3s?

Since you must make the songs available in your page HTML for the player to work, that means that any mp3s you link to are going to be fully downloadable by the user. I don’t know of a way around this. If you don’t want people downloading the audio, consider using a different player.

Update 6/10: Just got a comment from Lucas who posted a link to a method of obfuscating the mp3 location. The audio file is still downloadable, but you’d need to be somewhat web savvy to know how to get at it.

Hiding the Play/Pause Button

You can use the following CSS rules to hide the play/pause button:

body a.ymp-btn-page-play em.ymp-skin,
body a.ymp-btn-page-pause em.ymp-skin,
body .ymp-skin {
	display: none;
	}
body a.ymp-btn-page-play,
body a.ymp-btn-page-pause {
	outline: none !important;
	padding-left: 0 !important;
	position: relative !important;
	}

Local Problems

You may run into some problems testing the player on your computer.. as in the music files might not play. This is due to issues with Flash loading files from your local hard drive. Try adjusting your Flash settings to make things work out properly. If you can’t get it to work out, just have faith that when you test it out on your actual server the songs will play. Even if the songs don’t play, the player should at least show up, add mp3s to its playlist and display album art.

You can use audio and album art files that are located on other web sites. If you test out the example HTML in The Basics section, you’ll be using an audio and image file from the harmonicnw.com domain.

Further Reading

1

Adding Flickr Thumbnails to Your Web Page

June 5, 2009

Note: This post is one of a series of posts that breaks out the individual topics of my presentation “Pimp My Web Page” given at WebVisions 2009. You can see the fully pimped page that includes the code detailed below at http://pimpedblog.harmonicnw.com. Code tested for XHTML 1.0 Strict pages on IE 6 and 7 for PC, Firefox 3 for PC/mac, Safari 4 public beta for mac.

You can easily add Flickr thumbnails to your web page using Flickr’s web badge API. Insert the code below in your page where you want the thumbnails to appear:

<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=8&amp;display=random&amp;size=s&amp;source=user_set&amp;set=72157606336502682"></script>

Read on for details about adjusting the parameters to obtain the photos you want and styling the output with CSS.

Background

As with using the YouTube API, if you properly authenticate you can do all sorts of neat things with the Flickr API. The method I’m explaining here is a simple way to get your thumbnails without having to do any authentication. For most people, the functionality offered by the web badge API is probably enough. After all, it does allow you to pull up to ten photo thumbnails with a choice of sizing options from either a user account, a photo set or a group.

How It Works

This script makes a call to the Flickr API with a bunch of user parameters (i.e. count, display, etc.) In return, Flickr returns a bit of JavaScript that writes the requested thumbnails right to your page. Easy, huh?

Parameters

Specify parameters by changing the values after the ? in the script source URL. You can use these parameters to adjust number of thumbnails, size of thumbnails and source of the photos.

A word about size: if you choose the small square option (s), you will get a square that has cropped out the excess tallness or width from the original photo. With the other two options, you will get either a tall rectangle or a wide rectangle depending on the portrait/landscape orientation of the photo.

  • count – Number of thumbnails to show. Limit of 10 thumbnails.
  • display – Choose between random or latest.
  • size – Choose from the following options:
    • s – 75 pixels by 75 pixels
    • t – longest side is 100 pixels
    • m – longest side is 240 pixels
  • source – Choose from the following options:
    • user – Displays photos from a user. Specify which user by adding &user=[userid] where you specify the user ID.
    • user_set – Displays photos from a photo set. Specifiy which set by adding &set=[setid] where you specify the set ID.
    • group – Displays photos from a group. Specify which group by adding &group=[groupid] where you specify the group ID.
  • layout – This one is optional. If you want an alternate to the default HTML output, you can add &layout=x (see The HTML Output section below for details.) Otherwise just leave it out as I do in my examples. I believe the other “correct” options are h and v, but these both seem to result in the same default output.

Confusing? How about some examples. Let’s say you wanted to pull 5 random photos from the user bridgepix (user ID: 80651083@N00) in the largest size setting. You would use the following code:

<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=5&amp;display=random&amp;size=m&amp;source=user&amp;user=80651083@N00"></script>

Or if you wanted to pull the 3 latest photos from the Timbers Army group (group ID: 79221983@N00) in the smaller rectangular thumbnail size, use:

<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=3&amp;display=latest&amp;size=t&amp;source=group&amp;group=79221983@N00"></script>

So How Do I Locate those IDs?

You can find IDs for the user and photo sets within the URL of the photostream and photo set pages.

  • User ID – The user bridgepix’s photostream can be found at http://www.flickr.com/photos/80651083@N00/. The user ID is at the end of the URL (shown in bold here.)
  • Photo set IDbridgepix has a photo set at http://www.flickr.com/photos/80651083@N00/sets/72157606336502682/. The set ID is at the end of the URL (shown in bold here.)

The group ID isn’t quite as obvious.. it is hidden away from sight within the HTML. To find a group ID, browse to the Flickr group page, copy the group home page URL from your browser, then go to http://idgettr.com/ and paste the URL in the form. Hit the Find button to get the group ID. You can also use this site to get a user ID.

The HTML Output

If you do not specify layout=x in the parameters, then the HTML that the Flickr web badge API inserts into your page is structured like the example below:

<a href="http://www.flickr.com/photos/80651083@N00/2649621736/">
    <img width="75" height="75" title="USS Blueback Submarine" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3139/2649621736_e988c72421_s.jpg"/>
</a>
<a href="http://www.flickr.com/photos/80651083@N00/2740661357/">
    <img width="75" height="75" title="Steel Bridge, Portland, Oregon" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3248/2740661357_08c3a04671_s.jpg"/>
</a>
<a href="http://www.flickr.com/photos/80651083@N00/2656206031/">
    <img width="75" height="75" title="Submarine Propeller" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3120/2656206031_b52e379d2d_s.jpg"/>
</a>

If you had your Flickr API call within a <div> with the id photos, then you might add a stylesheet rule like #photos a img { margin: 0 5px } to add a margin between images. Or if you wanted to do away with the margins for all images including these thumbnails, you might use img { border: none }.

If you do specify layout=x in the API call, then you get slightly different HTML output that looks like:

<div id="flickr_badge_image1" class="flickr_badge_image">
    <a href="http://www.flickr.com/photos/80651083@N00/2660201203/">
        <img width="75" height="75" title="Morrison Bridge" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3133/2660201203_5b5939a851_s.jpg"/>
    </a>
</div>
<div id="flickr_badge_image2" class="flickr_badge_image">
    <a href="http://www.flickr.com/photos/80651083@N00/2665667372/">
        <img width="75" height="75" title="Morrison Bridge, Portland, Oregon" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3184/2665667372_40308a78a0_s.jpg"/>
    </a>
</div>
<div id="flickr_badge_image3" class="flickr_badge_image">
    <a href="http://www.flickr.com/photos/80651083@N00/2656206031/">
        <img width="75" height="75" title="Submarine Propeller" alt="A photo on Flickr" src="http://farm4.static.flickr.com/3120/2656206031_b52e379d2d_s.jpg"/>
    </a>
</div>

Making it W3C Valid

You should replace the @s in the URLs with %40s. And I’ve already encoded my &s with &amp;s in the examples. So to make that bridgepix user example fully XHTML 1.0 Strict W3C-compliant, use this code:

<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=5&amp;display=random&amp;size=m&amp;source=user&amp;user=80651083%40N00"></script>

Further Reading

1

Adding YouTube Thumbnails to Your Web Page

June 2, 2009

Note: This post is one of a series of posts that breaks out the individual topics of my presentation “Pimp My Web Page” given at WebVisions 2009. You can see the fully pimped page that includes the code detailed below at http://pimpedblog.harmonicnw.com. Code tested for XHTML 1.0 Strict pages on IE 6 and 7 for PC, Firefox 3 for PC/mac, Safari 4 public beta for mac.

To add YouTube thumbnails to your web page, insert this code just above </body>:

<!-- youtube -->
<script type="text/javascript">
// <![CDATA[

	function showYouTubeThumbnails(data) {
		/** USER SETTINGS ***********/
		var highResThumbnails = false;			// set to false for normal (120px x 90px), set to true for high-resolution (320px x 240px)
		var targetId = "videos";					// Id of element where the thumbnails will be inserted
		/******************************/

		var feed = data.feed;
	 	var entries = feed.entry || [];
	 	var targetObj = document.getElementById(targetId);

	 	for (var i = 0; i < entries.length; i++) {
	    		var entry = entries[i];
	    		var link = entry['link'][0]['href'];
	    		var thumbnailUrl;
			var thumbnailWidth;
			var thumbnailHeight;
			var minimumHighResWidth = 320;	// minimum width for a high-resolution thumbnail

			var thumbnailsObj = entry['media$group']['media$thumbnail'];
			for (var j = 0; j < thumbnailsObj.length; j++) {
				thumbnailUrl = thumbnailsObj[j]['url'];
				thumbnailWidth = thumbnailsObj[j]['width'];
				thumbnailHeight = thumbnailsObj[j]['height'];
				if (thumbnailsObj[j]['width'] >= minimumHighResWidth && highResThumbnails || thumbnailsObj[j]['width'] < minimumHighResWidth && !highResThumbnails) {
					break;
				}
			}

	    		var newA = document.createElement('a');
	    		newA.href = link;
	    		newA.title = "YouTube video";

	    		var newImg = document.createElement('img');
	    		newImg.src = thumbnailUrl;
	    		newImg.width = thumbnailWidth;
	    		newImg.height = thumbnailHeight;
	    		newImg.alt = "YouTube video thumbnail";
			newA.appendChild(newImg);

	    		targetObj.appendChild(newA);
	  	}
	}

// ]]>
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/users/TheFreeBoxShow/uploads?alt=json-in-script&amp;format=5&amp;callback=showYouTubeThumbnails&amp;max-results=5"></script>
<!-- end youtube -->

In that second JavaScript block, replace TheFreeBoxShow with the YouTube username of your choosing and change the max-results=5 to the number of thumbnails you want.  You’ll also need to add this <div> where you want the thumbnails to appear:

<div id="videos"></div>

Background

This script uses a YouTube API to retrieve information on videos posted by a specific user. This method is public, so you don’t need to authenticate, and it can be done entirely in JavaScript. There are a lot of other cool things you can do with the YouTube API if you sign up for an authentication key, but this is a quick and easy way to pull down thumbnails without having to do that.

How It Works

That second block of JavaScript makes a request to the YouTube API and passes a few key parameters: YouTube username and number of results. In return, the YouTube API passes back a JSON object (basically just a JavaScript object) that contains a bunch of information that we’ll use. The function in the first block of Javascript code, showYouTubeThumbnails(), sifts through the object to find the bits of information we need and then writes them to the videos <div>. Note that the showYouTubeThumbnails() function was designated in the API request in the second block of JavaScript. Also note that the designated function must be declared before the API call.

I hand coded the showYouTubeThumbnails() function. Feel free to modify that function to make it do exactly what you want. You may want to peruse the documentation about the returned object.

Two Thumbnail Sizes

My parsing function allows you to use either high or low resolution thumbnails. The high resolution thumbnails are 320 pixels by 240 pixels, the low resolution thumbnails are 120 pixels by 90 pixels. You can choose which size thumbnail to use and also the id of the DOM object where the HTML is added by setting the appropriate variables in the first few lines of the function showYouTubeThumbnails():

  • highResThumbnails – If set to true, the function uses high resolution thumbnails. If set to false, it uses low resolution thumbnails
  • targetId – Id of the DOM object where the thumbnails will be added

Other Parameters

You’ll want to specify the username and the max-results parameters in the API call, but there are a few other optional parameters that you might find useful such as start-index, orderby and category. Go here for details on other parameters you can use.

The HTML Output

The new HTML created by showYouTubeThumbnails() will be structured like the example code below:

<a href="http://www.youtube.com/watch?v=GPH-c_3pXSc" title="YouTube video">
	<img width="120" height="90" src="http://i.ytimg.com/vi/GPH-c_3pXSc/2.jpg" alt="YouTube video thumbnail"/>
</a>
<a href="http://www.youtube.com/watch?v=tPFCq67LeLI" title="YouTube video">
	<img width="120" height="90" src="http://i.ytimg.com/vi/tPFCq67LeLI/2.jpg" alt="YouTube video thumbnail"/>
</a>
<a href="http://www.youtube.com/watch?v=SmpKajP1Veo" title="YouTube video">
	<img width="120" height="90" src="http://i.ytimg.com/vi/SmpKajP1Veo/2.jpg" alt="YouTube video thumbnail"/>
</a>

You can customize this HTML with CSS. For example, if you wanted to put a margin between the images you might add a stylesheet rule like #videos img { margin: 0 5px }.

Further Reading

0