Tip: Play media files in a gadget

Tuesday, March 18, 2008 at 11:17 AM

Did you know you can insert a Windows Media Player into your Windows-only gadget to play music or even video clips? It takes just two easy steps:

Step 1: Paste the following code into main.xml within the <view> element.
classid = "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"
name = "WMPEmbed"
x = "" y = "" width = "100%" height = "100%"
enabled = "true">
<param name = "stretchToFit" value = "true"/>
<param name = "UIMode" value = "full"/> Full controls and screen
<param name = "autoStart" value = "true"/> Play media when open
<param name = "enableContextMenu" value = "true"/> Enable right-click menu
<param name = "URL" value=""/> URL of file to play
This first step sets up the Windows Media Player object in the gadget, giving it a name (WMPEmbed) so that JavaScript code has access to the object.

Step 2: Now paste the following code in main.js to add the ability to browse for and open different media files.
_wmp = WMPEmbed.object;

plugin.onAddCustomMenuItems = AddCustomMenuItems;

function AddCustomMenuItems(menu){
menu.AddItem("Open file", 0, getFile);

function getFile(){
var fileName = framework.BrowseForFile(
"Media Files |*.wmv;*.avi;*.mp3;*.wma;*.dat;|All Files|*.*");

if (fileName.length) {
_wmp.URL = fileName;
And you're finished! Use the "Open file" menu command to select a new media file. I used this technique in my Media Player gadget and Radio Donna gadget.

[Note from the editor: To play sound files in a gadget that works on both Windows and Mac, you can use the cross-platform API framework.audio.]

Have a tip you'd like to share? Send it to gd-developer AT google DOT com. To see all tips, choose the Tips label.

No comments: