How to Play Audio on Background in Windows Store Apps

In this article we explain how to play audio on background in Windows Store apps.

Introduction

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.

New-Project-Windows-Store-Apps.jpg

Step 2

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

Solution-Explorer-Windows-Store-Apps.jpg

Step 3

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

Add-Categories-Windows-Store-Apps.jpg

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".

Add-End-Pont-Windows-Store-Apps.jpg

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:

<Page

    x:Class="BackgroundAudioPlayer.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:BackgroundAudioPlayer"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <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"/>

    </Grid>

</Page>

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()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        { 

        } 

        private async void OpenFile_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                var FileOpen = new FileOpenPicker();

                FileOpen.SuggestedStartLocation = PickerLocationId.MusicLibrary;

                FileOpen.FileTypeFilter.Add(".MP3");

                FileOpen.FileTypeFilter.Add(".WMA");

                var File = await FileOpen.PickSingleFileAsync();

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

                MyPlayer.SetSource(Stream, File.ContentType);

                MyPlayer.Play();

            }

            catch (Exception ex)

            {

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

                msg.ShowAsync();

            }

        }

        private void PlayBtn_Click(object sender, RoutedEventArgs e)

        {

            MyPlayer.Play();

        } 

        private void PauseBtn_Click(object sender, RoutedEventArgs e)

        {

            MyPlayer.Pause();

        } 

        private void Stop_Click(object sender, RoutedEventArgs e)

        {

            MyPlayer.Stop();

        }

    }

}