Home > Windows Phone > How to stream video and audio from SkyDrive on Windows Phone

How to stream video and audio from SkyDrive on Windows Phone

In this post I will explain how to stream video and audio files from SkyDrive in MediaElement control or MediaPlayerLauncher. If you ever tried to play your media files from SkyDrive in your WP7 application using Live Connect API, you will probably get error message like “3002 AG_E_NOT_FOUND” or “Sorry, we can’t open this file on your phone“:

error1

error2

What is happening?

If you try to open your media file by pasting url in your desktop browser, you will notice that file url has format like:
http://storage.live.com/<folder_id>/MyVideo.wmv

But, when your default player starts streaming (e.g. Windows Media Player), you can see in playing file properties that it’s location is different; url is redirected to other location like:
http://frbedg.sn2.livefilestore.com/<folder_id>/MyVideo.wmv


On Windows Phone, for security reasons, the Silverlight runtime restricts access to certain classes of URLs classes in the System.Net and System.Windows.Controls namespace. These restrictions are designed to prevent networking threats (for example, threats based on a Silverlight application run from an internet server getting access to resources on a local intranet server). This means that MediaElement won’t redirect passed Source Uri value to other url address, and you will get error mentioned above. And this is not problem only with SkyDrive. You can experience this problem on other web-based hosting services.

The trick is to get second url by component that is not affected by this restriction, which is in our case WebBrowser control. All you have to do is to put WebBrowser control to your XAML page, set Visibility to Collapsed, and add handler to Navigated event:

<Grid x:Name="LayoutRoot" Background="Transparent" Margin="12,0,12,0">
 <MediaElement Name="movie" AutoPlay="True" Stretch="Uniform" />
 <phone:WebBrowser Name="webBrowser" Navigating="webBrowser_Navigating" Visibility="Collapsed" /> 
</Grid> 

Navigating event occurs every time before the WebBrowser control navigates to a new document. So, we only have to get uri when event triggers second time, and then set MediaElement Source property to that value, or launch MediaPlayerLauncher:

private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
    if (e.Uri.ToString() != file.Source)
    {
        e.Cancel = true;
        if (launchInControl)
            movie.Source = e.Uri;
        else {
            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
            mediaPlayerLauncher.Media = e.Uri;
            mediaPlayerLauncher.Controls = MediaPlaybackControls.All;
            mediaPlayerLauncher.Show();
        }
    }
}

And that’s it. Streaming will start. Happy coding.

Complete working zip solution can be downloaded from here (don’t forget to put your app client ID in MainPage.xaml).

Files

SamuraiJack

About these ads
  1. james
    April 25, 2012 at 11:56 pm | #1

    can show me the step how to do it?

  2. JD
    May 4, 2012 at 8:05 pm | #2

    Does not seem to work witn .mp4 videos, will only start playing after all video downloaded. Any idea why?

  3. December 18, 2012 at 6:44 am | #3

    Your style is unique compared to other people I’ve read stuff from. Thank you for posting when you have the opportunity, Guess I will just book mark this page.

  4. April 10, 2013 at 2:57 am | #4

    how would you do this in the background audio agent? I wasn’t able to instantiate a WebBrowser control, and I have my playlist full of redirected uris. i’m currently attempting to handle this by issuing a HEAD request and getting the resulting uri but it’s a pain!!

  5. April 25, 2013 at 2:58 pm | #5

    My spouse and I stumbled over here different page and thought I may
    as well check things out. I like what I see so now i’m following you. Look forward to finding out about your web page repeatedly.

  6. May 7, 2013 at 12:12 am | #6

    I’m curious to find out what blog system you are working with? I’m experiencing some small security issues with my latest website and I’d like to find something more safeguarded. Do you have any suggestions?

  7. May 10, 2013 at 11:39 pm | #7

    I’m gone to say to my little brother, that he should also go to see this weblog on regular basis to take updated from latest gossip.

  8. May 11, 2013 at 3:52 pm | #8

    I’m extremely inspired with your writing skills as neatly as with the format in your weblog. Is that this a paid subject matter or did you customize it yourself? Either way stay up the nice quality writing, it is uncommon to peer a nice weblog like this one today..

  9. Bev
    May 15, 2013 at 3:12 am | #9

    Hello there, You’ve done an excellent job. I will definitely digg it and personally suggest to my friends. I am sure they’ll be benefited from this
    site.

  1. March 9, 2012 at 11:54 am | #1
  2. July 24, 2012 at 5:57 pm | #2
  3. March 17, 2013 at 1:31 am | #3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: