Skip to content


Playing and Looping Audio

I recently completed my first Silverlight animation and I ended up stumbling with playing and looping an MP3 file for the animations background music. My first instinct was to add the MP3 file to the Solution but quickly found that Silverlight doesn't handle media file as an embed asset within its compiled XAP file but as an outside asset.

The Setup

To start, Silverlight requires you to place all media files inside your ClientBin. Here is a look at my projects structure.

/Build/ClientBin/Splash.xap
/Build/ClientBin/English.xap
/Build/ClientBin/French.xap
/Build/ClientBin/Music.mp3
/Build/Index.html
/Build/English.html
/Build/French.html

The XAML

We begin by adding the MediaElement node. Use the x:Name attribute to define its unique name and Source to define our MP3's location; the path is based on our folder structure and if I had my MP3 file within a folder called Media, /Build/ClientBin/Media/Music.mp3, the path would be Source="Media/Sound.mp3".

  1. <MediaElement x:Name="SoundTrack" Source="Music.mp3" />

By default the current XAML will only load our MP3 file with the XAP file so that it is accessible when your Silverlight application or animation starts. In order for us to hear our MP3, we can use the AutoPlay attribute to begin playing once the XAP file has finished loading.

  1. <MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" />

At this point if we build and run our project, we will hear our music playing in the background. If you're following this article with your own files, wait to the end of your track. You will notice that, by default all media files, play once.

Unfortunately XAML does not provide us with an attribute like Loop="True", therefore to accomplish a loop, we must use some C# code.

The Loop

To start, lets add the MediaEnded attribute to our MediaElement node; if you are using Visual Studio 2008 it will auto-compete a value of SoundTrack_MediaEnded and create the method, by the same name, in your C# code. If you are using Expressions Blend 2.5, who does not provide IntelliSense, you will need enter the value yourself and also go into you C# code and add the method SoundTrack_MediaEnded.

XAML

  1. <MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" MediaEnded="SoundTrack_MediaEnded" />

C#

  1. private void SoundTrack_MediaEnded (object sender, RoutedEventArgs e)
  2. {
  3.  
  4. }

Switch to your C# code and find SoundTrack_MediaEnded. As it stands right now, our XAML knows to call this function when our SoundTrack ends. In order to set a loop for our background music, we will need ensure we seek back to 0 and start its playback again.

  1. private void SoundTrack_MediaEnded (object sender, RoutedEventArgs e)
  2. {
  3.      SoundTrack.Stop();
  4.      SoundTrack.Play();
  5. }

If you build and run your project, you should have your audio file playing and looping.

The Outro

Make sure you check the documentation for a list all available attributes but one that you may need to use right away is the Volume attribute; uses a value from 0 to 1.

  1. <MediaElement x:Name="SoundTrack" Source="Music.mp3" AutoPlay="True" MediaEnded="SoundTrack_MediaEnded" Volume=".8"  />

Posted in Silverlight. Tagged with , , , , , .

Replace Your Incandescent Holiday Lights With Seasonal L.E.D. Lights

On the weekend I packed up my old Christmas lights and headed over to The Home Depot as they had a 2 day only Holiday Light Exchange event. The program was simple, bring in your old incandescent light strings and The Home Depot will give you a coupon for 50% off their regularly priced energy efficient Seasonal L.E.D string lights.

An Act of Green was created simply called Replace Your Incandescent Holiday Lights With Seasonal L.E.D. Lights. Although the event is over at The Home Depot, you can still find L.E.D lights at your local hardware store.

Posted in OMAoG. Tagged with , , .

One Million Acts of Green

Tonight, in partnership with CBC and The Hour with George Stroumboulopoulos, we launched One Million Acts of Green. The project is the brain child of Cisco, the idea behind the campaign is that one small act can make a big difference.

OMAoG is about mobilizing Canadians without overhauling their life; it's about one act from each individual amassing to a million. It can be as one simple as switching to compact fluorescent light bulbs, starting a recycling program, or walking to work. The site targets Canadians but is opened to our friends around the world.

OMAoG includes a green calculator designed by GreenNexxus that determines the impact of those acts on the environment.

One million acts of green, one act at a time.

Posted in Portfolio. Tagged with , , , .

Getting Started With phpFlickr

Today we are going to take a look at phpFlickr 2.2.0, a PHP class written by Dan Coulter in PHP4. It acts as a wrapper for Flickr's API and its methods return Flickr's XML response as well structured and simple to read arrays. There is a prerequisite for this article, you will need your own Flickr account and/or your own Flickr API.
 

The Setup

Our first step is to visit phpFlickr's homepage or its SourceForge project homepage and download the latest version; at the time of this article I downloaded release is 2.2.0. Next, create a root folder for this article, i called mine, Getting Started With phpFlickr, and extract the downloaded file into this folder. Finally, open your favorite editor and create two empty php files, account.php and example.php.
The Setup, Getting Started With phpFlickr
 

Your Account

Connecting to your Flickr account requires three separate IDs, your API's Key & Secret IDs and your account's User ID. Once you have applied for your own API, Flickr will show you your Key and Secret ID. Your User ID is simply your Flickr web address, http://www.flickr.com/photos/###; if you are like me and created a permanent link to your photostream without writing down that address down, you can use this tool, idGettr - Find your Flickr ID, to get it back. Once you have all three handy, open account.php, create three variables, $_key, $_secret, & $uid , and fill in your accounts' information; save and close your file.

  1. <?php
  2.  
  3. $_key = ""
  4. $_secret = "";
  5. $_uid = "";
  6.  
  7. ?>

 

Connecting To Flickr

We are now ready to use the phpFlickr class to connect to our account. Open example.php and, using include_once(), we begin by importing our account.php file and the class files, followed by making the initial connection to our account using the variable $_flickr.

  1. <?php
  2.  
  3. include_once("account.php");
  4. include_once("phpFlickr-2.2.0/phpFlickr.php");
  5. include_once("phpFlickr-2.2.0/PEAR/HTTP/Request.php");
  6.  
  7. $_flickr = new phpFlickr($_key, $_secret);
  8.  
  9. ?>

 

Your Photostream

As I mentioned in the beginning, a nice feature of phpFlickr is how its methods return Flickr's XML response as nice and simply arrays. For example, lets use the phpFlickr method photosets_getList($user_id = NULL) to connect to Flickr's API method, flickr.photosets.getList, who returns a list of each set in your photostream with its ID, description, number of photos and more. We will use the variable $_photosets to hold our array of information and print_r() to display the results.

  1. <?php
  2.  
  3. include_once("account.php");
  4. include_once("phpFlickr-2.2.0/phpFlickr.php");
  5. include_once("phpFlickr-2.2.0/PEAR/HTTP/Request.php");
  6.  
  7. $_flickr = new phpFlickr($_key, $_secret);
  8. $_photosets = $_flickr->photosets_getList($_uid);
  9. print_r($_photosets);
  10.  
  11. ?>

 
Once you are ready, save your file, upload and/or open example.php on your live or local server. If you have entered the correct information inside account.php, you will see something like mine when choosing View Source on your browser.

  1. Array
  2. (
  3.     [photoset] => Array
  4.         (
  5.             [0] => Array
  6.                 (
  7.                     [id] => 72157607910181389
  8.                     [primary] => 2928529397
  9.                     [secret] => 60a692493f
  10.                     [server] => 3070
  11.                     [farm] => 4
  12.                     [photos] => 2
  13.                     [videos] => 0
  14.                     [title] => Baby Myles Alexander
  15.                     [description] => Pictures of Myles from birth to 12 months
  16.                 )
  17.  
  18.         )
  19.  
  20. )

 

The Outro

This class does require some knowledge of how Flickr's API methods work, what they return and what they require. Thankfully, you can open phpFlickr.php inside our phpFlickr-2.2.0 folder to look at all the methods available in this class. Dan Coulter has commented the associated Flickr API address for you to reference and read more about what that method.

  1. function photosets_getList($user_id = NULL)
  2. {
  3.     /* http://www.flickr.com/services/api/flickr.photosets.getList.html */
  4.     $this->request("flickr.photosets.getList", array("user_id" => $user_id));
  5.     return $this->parsed_response ? $this->parsed_response['photosets'] : false;
  6. }

Posted in PHP. Tagged with , , .

Local Database

A friend learning PHP, recently asked me how they can mimic a database when the site they are working on does not have access to one. The following will show you how we can use an Array to create a local database and nested Arrays to create tables, rows and columns as key/value pairs.
 

The Source

The data for today comes from MySQL 5.0 Manual, 3.3.3. Loading Data into a Table.

name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1979-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29

 

The Skeleton

We begin by creating an array that represents our database, $db = array (). We follow that by defining our table, "pets" => array () as key/value pairs. Our tables' name is set as the key and an empty array is set as its value.

  1. <?php
  2.  
  3. $db = array (
  4.     "pets" => array (
  5.    
  6.     )
  7. );
  8.  
  9. ?>

 

Adding The First Row

Using the data from our source, lets add our first row. Each row will be represented by an array with key/value pairs; the column name of our table is set as the key and the rows value for that column is set as the value..

  1. <?php
  2.  
  3. $db = array (
  4.     "pet" => array (
  5.         array (
  6.             "name" => "Fluffy",
  7.             "owner" => "Harold",
  8.             "species" => "cat",
  9.             "sex" => "f",
  10.             "birth" => "1993-02-04",
  11.             "death" => ""
  12.         )
  13.     )
  14. );
  15.  
  16. ?>

 
Continue adding the rest of the data from our source. To differentiate each row, ensure you place a comma between each array but not on the last array. Once you are done adding each row, save your file as db.php. Here a quick look at three completed rows.

  1. <?php
  2.  
  3. $db = array (
  4.     "pet" => array (
  5.         array (
  6.             "name" => "Fluffy",
  7.             "owner" => "Harold",
  8.             "species" => "cat",
  9.             "sex" => "f",
  10.             "birth" => "1993-02-04",
  11.             "death" => ""
  12.         ),
  13.         array (
  14.             "name" => "Claws",
  15.             "owner" => "Gwen",
  16.             "species" => "cat",
  17.             "sex" => "m",
  18.             "birth" => "1994-03-17",
  19.             "death" => ""
  20.         ),
  21.         array (
  22.             "name" => "Buffy",
  23.             "owner" => "Harold",
  24.             "species" => "dog",
  25.             "sex" => "f",
  26.             "birth" => "1989-05-13",
  27.             "death" => ""
  28.         )
  29.     )
  30. );
  31.  
  32. ?>

 

Importing The Data

Now that db.php is ready, lets continue by creating a new file, showPets.php; it will be this file that we will import our database and show its results. Lets begin by importing our database onto our new file, include_once("db.php") and to ensure our array is looking like a database, lets print the results using print_r().

  1. <?php
  2.  
  3. include_once("db.php");
  4. print_r($db);
  5.  
  6. ?>

 

View The Results

Upload or if you have a local web server running, open showPets.php with your preferred web browser; i hope your preferred browser is not IE. If you're $db array has no syntax errors, you will see...

  1. Array ( [pet] => Array ( [0] => Array ( [name] => Fluffy [owner] => Harold [species] => cat [sex]
  2. => f [birth] => 1993-02-04 [death] => ) [1] => Array ( [name] => Claws [owner] => Gwen
  3. [species] => cat [sex] => m [birth] => 1994-03-17 [death] => ) [2] => Array ( [name] => Buffy
  4.  [owner] => Harold [species] => dog [sex] => f [birth] => 1989-05-13 [death] => ) [3] => Array
  5. ( [name] => Fang [owner] => Benny [species] => dog [sex] => m [birth] => 1990-08-27 [death]
  6. => ) [4] => Array ( [name] => Bowser [owner] => Diane [species] => dog [sex] => m [birth] =>
  7.  1979-08-31 [death] => 1995-07-29 ) [5] => Array ( [name] => Chirpy [owner] => Gwen [species]
  8.  => bird [sex] => f [birth] => 1998-09-11 [death] => ) [6] => Array ( [name] => Whistler [owner]
  9.  => Gwen [species] => bird [sex] => [birth] => 1997-12-09 [death] => ) [7] => Array ( [name] =>
  10.  Slim [owner] => Benny [species] => snake [sex] => m [birth] => 1996-04-29 [death] => ) ) )

 
To view a structured version of the results, go to View Source.

  1. Array
  2. (
  3.     [pet] => Array
  4.         (
  5.             [0] => Array
  6.                 (
  7.                     [name] => Fluffy
  8.                     [owner] => Harold
  9.                     [species] => cat
  10.                     [sex] => f
  11.                     [birth] => 1993-02-04
  12.                     [death] =>
  13.                 )
  14.  
  15.             [1] => Array
  16.                 (
  17.                     [name] => Claws
  18.                     [owner] => Gwen
  19.                     [species] => cat
  20.                     [sex] => m
  21.                     [birth] => 1994-03-17
  22.                     [death] =>
  23.                 )
  24.  
  25.             [2] => Array
  26.                 (
  27.                     [name] => Buffy
  28.                     [owner] => Harold
  29.                     [species] => dog
  30.                     [sex] => f
  31.                     [birth] => 1989-05-13
  32.                     [death] =>
  33.                 )
  34.  
  35.             [3] => Array
  36.                 (
  37.                     [name] => Fang
  38.                     [owner] => Benny
  39.                     [species] => dog
  40.                     [sex] => m
  41.                     [birth] => 1990-08-27
  42.                     [death] =>
  43.                 )
  44.  
  45.             [4] => Array
  46.                 (
  47.                     [name] => Bowser
  48.                     [owner] => Diane
  49.                     [species] => dog
  50.                     [sex] => m
  51.                     [birth] => 1979-08-31
  52.                     [death] => 1995-07-29
  53.                 )
  54.  
  55.             [5] => Array
  56.                 (
  57.                     [name] => Chirpy
  58.                     [owner] => Gwen
  59.                     [species] => bird
  60.                     [sex] => f
  61.                     [birth] => 1998-09-11
  62.                     [death] =>
  63.                 )
  64.  
  65.             [6] => Array
  66.                 (
  67.                     [name] => Whistler
  68.                     [owner] => Gwen
  69.                     [species] => bird
  70.                     [sex] =>
  71.                     [birth] => 1997-12-09
  72.                     [death] =>
  73.                 )
  74.  
  75.             [7] => Array
  76.                 (
  77.                     [name] => Slim
  78.                     [owner] => Benny
  79.                     [species] => snake
  80.                     [sex] => m
  81.                     [birth] => 1996-04-29
  82.                     [death] =>
  83.                 )
  84.  
  85.         )
  86.  
  87. )

 

Posted in PHP. Tagged with , , .