17
Mar
12

metro background audio c# (consumer preview)

Peter Daukintis

Some changes occurred in the consumer preview release of Windows 8 regarding background audio; the steps required to get it working are outlined in this forum post

http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/2d3496df-a145-4d87-be08-aadd5a8098e2

The steps are

  • Create a MediaElement and set it’s AudioCategory property to ‘BackgroundCapableMedia’
    <MediaElement x:Name="myMedia"
                  Source="/Assets/Sleep Away.mp3"
                  AudioCategory="BackgroundCapableMedia"/>

  • Update the app manifest to declare Background Tasks of types ‘Audio’ and ‘Control Channel’
  • Implement event handlers for
    • MediaControl.PlayPressed
    • MediaControl.PausePressed
    • MediaControl.PlayPauseTogglePressed
    • MediaControl.StopPressed

 

public BlankPage()
{
    this.InitializeComponent();
    MediaControl.PlayPressed += MediaControl_PlayPressed;
    MediaControl.PausePressed += MediaControl_PausePressed;
    MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed;
    MediaControl.StopPressed += MediaControl_StopPressed;
}

private void MediaControl_StopPressed(object sender, object e)
{
    myMedia.Stop();
}

private void MediaControl_PlayPauseTogglePressed(object sender, object e)
{
}

private void MediaControl_PausePressed(object sender, object e)
{
    myMedia.Pause();
}

private void MediaControl_PlayPressed(object sender, object e)
{
    myMedia.Play();
}

The media transport event handlers which need to be implemented are detailed here http://msdn.microsoft.com/en-us/library/windows/hardware/hh833781.aspx

 

Here’s a working sample project.

https://skydrive.live.com/redir.aspx?cid=4f1b7368284539e5&resid=4F1B7368284539E5!440&parid=4F1B7368284539E5!123‏

UPDATE: I have updated this for the Release Preview (see http://babaandthepigman.wordpress.com/2012/08/12/metro-background-audio-c-release-preview/)

About these ads

12 Responses to “metro background audio c# (consumer preview)”


  1. April 25, 2012 at 10:57 am

    Hi, I downloaded your sample project but found the “pause” button didn’t work when I press the volume button. Also, I followed the steps you described and ran into the same problem. confused…

    • May 14, 2012 at 6:24 pm

      Probably because it’s hitting this MediaControl_PlayPauseTogglePressed handler which has no code in it? That was the case for me so I simply put toggling code in there based on the Status.

      • 3 tarun
        May 23, 2012 at 11:08 am

        hi, Can you share which code you added for toggling. I am trying to mymedia inside that event and it is giving me UI Thread Error.

      • May 23, 2012 at 2:04 pm

        @tarun, yes you will get that problem, the event doesn’t come from the UI thread.
        You need to schedule it on the dispatcher….

        private void MediaControl_PlayPauseTogglePressed(object sender, object e)
        {
        Dispatcher.InvokeAsync(CoreDispatcherPriority.High, (s, args) =>
        {
        if (CurrentState != MediaElementState.Playing)
        {
        Play();
        }
        else
        {
        Pause();
        }
        },
        this, null);
        }

  2. 5 steve
    July 18, 2012 at 11:16 am

    Hi,

    Unfortunately, this sample does not work for the Release Preview. I have built a sample using the same settings in RP but the media pauses working when you navigate to another app i.e. does not play in background. Any ideas?

  3. December 1, 2012 at 10:24 am

    Outstanding post however I was wondering if you could write a litte more on
    this subject? I’d be very grateful if you could elaborate a little bit more. Thank you!


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: