Using FindService and PlayMedia to change the TV channel in Media Center

If you've used the MSN TV pages on Online Spotlight in Media Center you might have noticed buttons on some pages to tune to MSNBC or to The Weather Channel.  The page uses the Media Center API FindService to find which channel MSNBC is in your particular channel line up and then uses PlayMedia to change the channel (including starting TV if it is not currently in use).  Here's a script sample that shows how it works:

 function watchMSNBC()
{
    var rgServiceIDs = MCE.FindService("msnbc", ""); 
    if (rgServiceIDs.Length == 1) 
    { 
         MCE.PlayMedia(0, rgServiceIDs(0));
    }
    else 
    {
        // MSNBC isn't in the channel listing (or there are multiple channels called MSNBC)
    } 
}

First of all the code calls FindSerivce with the callsign of the channel, in this case "msnbc", the second parameter is used to find a term in the description of the channel - you'd use this if you wanted to tune to a channel that you didn't know the callsign of - for example you might want to tune to the local ABC channel without having to know the callsign for the channel in each local market.  FindService returns an array of channels that match the required attributes; in this case we're assuming that only one channel will be found.   Finally we start the channel playing using PlayMedia.  The first parameter is the type of media we want to start playing, in this case 0 for TV.  The second parameter is the ID of the channel that we want to tune to.  Instead of tuning to a particular channel using FindService it's possible to tune to a particular show if it is currently playing using FindProgram, but more on that another time.

This posting is provided "AS IS" with no warranties, and confers no rights.