How to Play Audio on Background in Windows Store Apps


In Windows Store apps, to play audio and video files you use the MediaElement control. But when you try to play an application in the background or switch to another application then the audio stops and you are unable to hear the audio. In this article we explain how to play a media file in the background. If you playing a video and switch to background mode then you will hear the audio portion but not see the accompanying video.

How to play a media file in the background in Windows Store apps

Step 1

Start Visual Studio 2012 and create a new Windows Store apps project.


Step 2

Go to Solution Explorer and double-click on "Package.appxmanifest" to open it.


Step 3

Go to the "Declarations" tab and select "Background Tasks" from the "Available Declarations" dropdown and click on "Add".


Step 4

In this step click on the "Audio" checkbox in "Properties" and add the Entry point. An End point is added to the project name with ".App". For example if your project is named "BackgroundAudioPlayer" then that becomes "BackgroundAudioPlayer.App".


Step 5

Go to Solution Explorer and click on "MainPage.xaml" and add the AudioCategories attribute to the MediaElement. Set the attribute to either  "Communications" or "BackgroundCapabaleMedia".

<MediaElement  x:Name="MyPlayer" AudioCategory="Communications" Source="MySong.mp3" />

Step 6

Here is the complete "MainPage.Xaml" code:









    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <MediaElement  x:Name="MyPlayer" AudioCategory="Communications" Source="MySong.mp3" />

        <Button Content="OpenFile" x:Name="OpenFile" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="579,451,0,0" Click="OpenFile_Click"/>

        <Button Content="Play" x:Name="PlayBtn" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="703,451,0,0" Click="PlayBtn_Click"/>

        <Button Content="Pause" x:Name="PauseBtn" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="800,451,0,0" Click="PauseBtn_Click"/>

        <Button Content="Stop" x:Name="Stop" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="906,451,0,0" Click="Stop_Click"/>



Step 7

Here is the complete "MainPage.xaml.cs" code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.Storage;

using Windows.Storage.Pickers;

using Windows.UI.Popups;


namespace BackgroundAudioPlayer


    public sealed partial class MainPage : Page


        public MainPage()




        protected override void OnNavigatedTo(NavigationEventArgs e)



        private async void OpenFile_Click(object sender, RoutedEventArgs e)




                var FileOpen = new FileOpenPicker();

                FileOpen.SuggestedStartLocation = PickerLocationId.MusicLibrary;



                var File = await FileOpen.PickSingleFileAsync();

                var Stream = await File.OpenAsync(FileAccessMode.Read);

                MyPlayer.SetSource(Stream, File.ContentType);



            catch (Exception ex)


                MessageDialog msg = new MessageDialog("Plase Select any media file");




        private void PlayBtn_Click(object sender, RoutedEventArgs e)




        private void PauseBtn_Click(object sender, RoutedEventArgs e)




        private void Stop_Click(object sender, RoutedEventArgs e)






Up Next
    Ebook Download
    View all
    View all