Search code examples
.netwindows-8microsoft-metromediaelementwinrt-async

WinRT C# Metro Play MediaElement Asynchronous


Im trying to build Win8 metro style application. Im trying to play remote video which is on web in media element.

      MediaElement media = new MediaElement();
        Uri url = new Uri("some url on web");
        media.Source = url;
        media.Play();

The first streaming takes time. It takes time to video to start play, in this meantime application is locked. I want to do that playing ascnhronously. How can i achive this.


Solution

  • after some search, i find out that with dispatcher element, i can do my work asynchronously. Here is the sample:

    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
             {
                MediaElement media = new MediaElement();
                Uri url = new Uri("some url on web");
                media.Source = url;
                media.Play();
             }
    
            );
    

    You have to mark the calling function as async in order this code block to work.