Archive

Posts Tagged ‘MediaPlayerLauncher’

How to stream video and audio from SkyDrive on Windows Phone

March 7, 2012 8 comments

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

How to crash other WP7 applications?

February 20, 2012 6 comments

Do you know what is easiest way to crash someone’s WP7 application? Just quickly double-tap it’s “Rate my app” link or button. Marketplace Review page will display, but when you go back to app, it will disappear because it crashed.

Of course some app’s has this issue properly resolved, most not, but what happens when you double tap button or link that starts any kind of WP7 Launchers? Simply, your app will try twice to launch task, but second launch will throw “Navigation is not allowed when the task is not in the foreground” exception, because your app is not anymore active on phone.

Here is example. Create new Windows Phone application, uncomment ApplicationBar section at the end of MainPage.xaml and leave one ApplicationBarIconButton:

<phone:PhoneApplicationPage.ApplicationBar>
 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
 <shell:ApplicationBarIconButton IconUri="/Home.png" Text="navigate"/>
 </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar> 

In MainPage.xaml.cs add following code:

ApplicationBarIconButton appBarButton;

public MainPage()
{
    InitializeComponent();
    appBarButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
}

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This app will launch the web browser application and display the Bing web site. Start app and double-tap application bar icon button. Following unhandled exception will occur:

appbarcrash

This will happen in most situation where you are trying to start any of available launchers such as Web Browser Task, Share Link Task, Marketplace Review Task, etc. There are several ways to handle those situations:

1. Add task.Show() method in try/catch block:

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    try {
        task.Show();
    }
    catch {}
}

2. Disable button on first click:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
    if ( e.NavigationMode == NavigationMode.Back )
        appBarButton.IsEnabled = true;
}

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.IsEnabled = false;
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This will disable button, so second click won’t happen, but you also have to re-enable button when user goes back to this page.

3. Remove Click handler:

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.Click -= new EventHandler(appBarButton_Click);
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

Happy coding!