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.

Comments (12)

  1. John says:

    Thank you, Michael, for this code!

    Can anyone tell me:

    Can I run the HTML page directly, or do I have to run it through an .MCL file?

    Is there anything the code is missing to run as is? Can anyone please provide that code?

    Where this code should be run?

    For example, can I call this function, "watchMSNBC()", onload of an HTML page, for example?

    Thanks a bunch!

    John

  2. John,

    You can’t open a HTML page directly in Media Center, you need a MCL to open a page, once you have an open page though you can open other pages (for example via JScript or a user clicking a link).

    This code is missing one thing I’ve realised – a definition of the MCE object – you’ll need to add a line that defines it thus:

    var MCE = window.external.MediaCenter();

    You could run this code from the onLoad function of an HTML page, although I’d recommend not doing that – you want your users to have a choice to start the TV or not – they may be watching something already and wouldn’t appreciate the interuption!

  3. Efrat says:

    Hi Mike,

    Thanks for the code =)

    I’m actually having trouble getting the Guide to populate with data. MC does not find data for *any* valid zip code I feed it at the Guide update/setup. I’ve tried 91329, 91436, 90210 and even the example zip code, 98052, and nothing works.

    As a result, I suppose, I can’t get your code to work. The error I get is "Length is null or not an object". Perhaps the empty Guide is not the cause of this particular error, but that is probably beside the point.

    Of course, I would appreciate any help on getting the data for any zip, but until then, it would be great if I could tune to a channel by channel number instead of by call sign or serviceID. That I should be able to do without the Guide being populated, right? I am also trying to find out the sequential channel changing function (channel up/down) and the direct channel changing function (channel xxx).

    Thanks again,

    Efrat

  4. You’ll need the guide to be download to use FindService and FindProgram.

    If you’re having difficult downloading the guide, try disabling any popup blockers you have installed – some use a proxy that has caused problems for some people. I’ve also heard of some people having problems because their firewall was blocking the guide download.

    You can’t programatically change channel up or down or tune to a numbered channel – this is because channel numbers of change between services and locations, so although the channel you want to tune to might be channel 13, it probably won’t be that for everyone, hence changing by call sign or description is more reliable.

  5. Efrat says:

    Thanks again for your replay =)

    Yet the Media Center allows the user to tune by local channel number and by up/down. I think it would be nice if the SDK allowed for this too.

    Efrat

  6. Jamie says:

    Hi,

    I don’t know if anybody can help but i can’t seem

    to pick up a channel through the aerial port while playing through media center, it automatically searches for the received channels and manages to pick up 3/5 available. when i hook the aeriel up to a normal t.v all channels are found.. any suggestions..?

    Jamie

  7. Jamie says:

    Hi,

    I don’t know if anybody can help but i can’t seem

    to pick up a channel through the aerial port while playing through media center, it automatically searches for the received channels and manages to pick up 3/5 available. when i hook the aeriel up to a normal t.v all channels are found.. any suggestions..?

    Jamie