1. I'm one to play favorites, and who isn't? That's why it's called a favorite, am I right? And my favorite contract for Windows 8 is the PlayTo contract. The PlayTo contract allows you to use the Digital Living Network Alliance (DLNA) standard to stream Audio, Video, or Image content to any DLNA enabled device on the network. The most common use of the PlayTo contract will be using your App as a source for the media you would like to see or hear on another device. Perhaps you have a great set of speakers or a beautiful 80" flat screen TV, with the PlayTo contract you can send content to those things right from your Windows 8 device whether it is a Tablet, Ultrabook, Desktop or All-In-One PC on Windows 8 and Windows RT.

    Use Cases

    So how can you use PlayTo in your app? First I would say that any app that displays video, plays music or other audio content, or shows pictures is a candidate to use the PlayTo contract. It will not be appropriate for all scenarios and every user will not always use it, but if you think someone might find it useful under some conditions, go ahead and include it. PlayTo has some obvious applications if you were making a YouTube/Vimeo/Viddler type of application, an MP3 Music Player app, or an Image Slide show but like I said previously PlayTo should be considered for use in any application that has Video/Audio/Image content.

    Let's take Games for example, we all love to play games, and let's be honest a game soundtrack is really important to the experience a player will have while playing. The music can be used to create a sense of urgency when time is running out or to otherwise set the mood and entertain during gameplay. So why not allow your users to stream your game soundtrack from their tablet to their awesome stereo system without any wires?

    Take a moment, put on your creative hat, and think about ways your users might be able to leverage PlayTo in your application. PlayTo may not be right for your application, but I think in many cases it will, I'm sure there are many uses well beyond the obvious. Do you have some clever ideas on how to use PlayTo? Feel free to let us all know in the comments!

    Running in the Background

    When a Windows Store application on Windows 8 is moved to the background it would normally be suspended and stop execution. Not so if you have an active PlayTo connection! Your application will continue to run and your content will continue to be sent to your DLNA device.

    This has a couple of implications for the developer, if you have implemented this in a game like I described above you might want to pause the gameplay since it won't be easy for user to play it without it being on the screen. You don't want their character to die or for them to lose their place because they moved your game to the background not realizing it would not suspend as it normally would because of the PlayTo Connection. There might be some scenario where you want that processing to continue, but it's something to think about for your implementation.

    For a video player or an MP3 player, this is a great behavior because you can then continue to use your Windows 8 device with full functionality to open more apps, you could even have 2 apps open in snapped and filled view state with an active PlayTo enabled application running in the background off screen completely.

    Limitations

     PlayTo is not without limits, as you can only select one PlayTo destination at a time. I had a thought to use PlayTo and create a whole house DJ app where you could send arrange playlists for different rooms at a House Party all from one app on a single device. This is not possible with PlayTo as it only allows you to set a single destination. You may also run into instances where PlayTo is not allowed because of DRM restrictions on the content itself, but this is the expected behavior of applications respecting DRM.

    The Guts

    Microsoft has provided some excellent documentation, tutorials/quickstarts, and sample code for developers of Windows 8 applications. I've been very impressed with their efforts on this. I think the best place to start for this discussion is with the Quickstart documents. My only criticism of these 2 is that they included a lot of context in the instructions, but that is probably a good thing for many folks. For others you might want to just skip down to Step 5, the other steps are more related to the normal things you would have to do to play video/audio/image content in your application and are not specific to PlayTo. For experienced developers, I would suggest you take a look at Step 5 first, if it makes sense to you then run with it, if not go back to step 1 and you will get the entire experience.

    Javascript and HTML5
    http://msdn.microsoft.com/en-us/library/windows/apps/hh465184.aspx

    VB/C#/C++ and XAML
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh465191.aspx


    This is pretty much all the code you need in an HTML5/JS app to implement the PlayTo contract. Where mediaElement is any img, audio, or video DOM element. It's that easy. Really! Once you add this code to your application, the user would select a DLNA device from the devices charm and next thing you know PlayTo is in action.


    var ptm = Windows.Media.PlayTo.PlayToManager.getForCurrentView();
    ptm.addEventListener("sourcerequested", sourceRequestHandler, false);

    function sourceRequestHandler(e) {
        try {
            var sr = e.sourceRequest;
            var controller;
            try {
                controller = mediaElement.msPlayToSource;
            } catch (ex) {
                //Handle and Report PlayTo failure
            }
            sr.setSource(controller);
        } catch (ex) {
            //Handle and Report exceptions
        }
    }

     

    For C#/XAML applications the process is quite similar, though a bit longer and would apply to the MediaElement and Image XAML tags being assigned as the mediaElement object elsewhere in the code. Again if you require the full explanation simply follow the quickstart from Step 1.



    private Windows.Media.PlayTo.PlayToManager ptm;

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        ptm = Windows.Media.PlayTo.PlayToManager.GetForCurrentView();
        ptm.SourceRequested += SourceRequested;

    }
    private Windows.UI.Core.CoreDispatcher dispatcher = Window.Current.CoreWindow.Dispatcher;

    async private void SourceRequested(Windows.Media.PlayTo.PlayToManager sender,
                                 Windows.Media.PlayTo.PlayToSourceRequestedEventArgs e)
    {
        await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            try
            {
                Windows.Media.PlayTo.PlayToSourceRequest sr = e.SourceRequest;
                Windows.Media.PlayTo.PlayToSource controller = null;
                Windows.Media.PlayTo.PlayToSourceDeferral deferral =
                    e.SourceRequest.GetDeferral();
                try
                {
                    if (mediaElement is Image)
                    {
                        controller = ((Image)mediaElement).PlayToSource;
                    }
                    else
                    {
                        controller = ((MediaElement)mediaElement).PlayToSource;
                    }
                }
                catch (Exception ex)
                {
                    MessageBlock.Text += "Exception encountered: " + ex.Message + "\n";
                }
                sr.SetSource(controller);
                deferral.Complete();
            }
            catch (Exception ex)
            {
                MessageBlock.Text += "Exception encountered: " + ex.Message + "\n";
            }
        });
    }



    The Other Direction


    Until this point in this post I have been focused on using the PlayTo Contract as a media source. However, you can also implement a PlayTo receiver. This will allow other devices and apps that can act as a source to send their content to your application. It doesn't have some of the same limitations as the PlayTo source, you can have multiple PlayTo receivers in a single application. So if you wanted to do some sort of Picture in Picture video playing with a completely separate audio track and an image slide show up in the corner all at once you could do that. Within the reasonable bounds of your networking hardware of course.

    I won't be going into the code of the PlayTo receiver as it's a bit more lengthy, I will point you to the quickstarts again:

    HTML5/JS
    http://msdn.microsoft.com/en-us/library/windows/apps/Hh967763.aspx

    VB/C#/C++ and XAML
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh967764.aspx



    Conclusion


    As you now know, the PlayTo Contract can unlock some great use cases in your application in exchange for adding just a little bit of code. If you have audio/video/image content in your application I feel like it's a no-brainer to add PlayTo in most cases. I've added into just about all of my Windows 8 applications. I'm sure it's not getting used all the time, many people will be oblivious to it's existence until society gets more familiar with the common features of Windows Store applications on Windows 8. Until then you may want to call out the feature in your UI and help pages. You might even include a link to the PlayTo Troubleshooting page so you don't get bad reviews in the Windows Store because of the users failure to set things up properly for DLNA sharing.  . I hope you enjoy using the PlayTo contract as much as I do, after all it is my favorite.


    Here are some apps in which I have implemented PlayTo:

    Source


    Pub Quiz: http://apps.microsoft.com/windows/en-US/app/a3064295-12cb-4938-bfb7-be738ae98843

    Run and Shoot http://apps.microsoft.com/windows/en-US/app/6443b236-33bf-4fdb-ac98-8f9f2d1148bd

    Tweaker (the Tweet Speaker) http://apps.microsoft.com/windows/en-US/app/330612cd-83b0-4b8b-9cd5-6e196dfc980d

    The Holy Bible http://apps.microsoft.com/windows/en-US/app/4d028eea-8bd4-4dc4-961c-52e3dc10e70c

    Receiver 

    PlayTo Receiver: http://apps.microsoft.com/windows/en-US/app/72a6ba17-2d4e-4a1c-bcfb-cdc5d4b32d0e

    Pub Quiz: http://apps.microsoft.com/windows/en-US/app/a3064295-12cb-4938-bfb7-be738ae98843
Blog Archive
About Me
About Me
Labels
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.