MediaElement, MediaPlayerLauncher and ApplicationBar in Windows Phone

Today, I'll write about creating a simple app that plays media using the MediaElement control, uses the ApplicationBar control for the media controls, and also allows use of the MediaPlayerLauncher. 

The application bar icons used can be downloaded for free at: 

First, create a new Windows Phone Application project.  Add the files for the ApplicationBar icons to the project for play and pause, and change their Build Action to "Content".


Then update the xaml MainPage.xaml to add a MediaElement and the ApplicationBar:

    <Grid x:Name="LayoutRoot" Background="Transparent">


            <RowDefinition Height="*"/>


        <MediaElement Source="" AutoPlay="True" x:Name="mediaElement1" />



        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="" Text="Play" Click="appbar_button1_Click"/>

            <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="" Text="Pause" Click="appbar_button2_Click"/>


                <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="Media Player Launcher" Click="menuItem1_Click" />





Finaly, update the code in MainPage.xaml.cs to hook up the Play, Pause and MediaPlayerLauncher events:

        MediaPlayerLauncher mediaPlayerLauncher;

        public MainPage()



            mediaPlayerLauncher = new MediaPlayerLauncher();


        private void appbar_button1_Click(object sender, EventArgs e)




        private void appbar_button2_Click(object sender, EventArgs e)




        private void menuItem1_Click(object sender, EventArgs e)


            mediaPlayerLauncher.Media = new Uri("", UriKind.Absolute);




 To enable the app to run in landscape mode, change the SupportedOrientations in MainPage.xaml to PortraitOrLandscape: