thongaduka
thongaduka

Reputation: 627

How to play background music online in Windows 8

In my application, I use UI MediaElement. But when i click the Windows key, the music stops.

I tried using:

MediaControl.PlayPressed += MediaControl_PlayPressed;
        MediaControl.PausePressed += MediaControl_PausePressed;
        MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed;
        MediaControl.StopPressed += MediaControl_StopPressed;

I set source MediaElement:

media.Source = new Uri("http://stream-hq.mp3.zdn.vn/fsgggsfdlwjglwjAAAAA/2a3f830202ea6d29bc7c5a5146401566/4ff5620a/2011/12/27/a/4/a4fcc199a184a93cfeb0fe35642c53bf.mp3", UriKind.RelativeOrAbsolute);

Please help me!

Upvotes: 8

Views: 4529

Answers (2)

akton
akton

Reputation: 14386

For a Metro/WinRT app to play audio in background, the app needs the following:

  1. A MediaElement control that:
    1. Is in a XAML page.
    2. The AudioCategory property set to BackgroundCapableMedia (as in Armando's answer). There are other values for games or communication systems as needed. See the Audio Playback in a Metro Application for information on what the different options mean.
  2. Use the MediaControl object to capture at least the following. Other events and properties can be handled if desired but the following are required for background playback to function.
    1. PlayPressed
    2. StopPressed
    3. PlayPauseTogglePressed
    4. PausePressed
  3. Add audio to the list of support background tasks in the applications manifest. The manifest is usually called Package.appxmanifest. Select it in the Solution Explorer, go to the Declarations tab and check "Audio" as shown:

enter image description here

See the Transport Controls Guide for more info about capturing hardware buttons (e.g. play/pause on the keyboard) and the quickstart guide for creating a media player for more info.

Upvotes: 10

Armando
Armando

Reputation: 11

This would be my first answer. Make sure you set AudioCategory="BackgroundCapableMedia" in your XAML like this:

<MediaElement x:Name="backgroundMusic" 
              AutoPlay="True" 
              AudioCategory="BackgroundCapableMedia" 
              Source="mms://betafm.santafe-conicet.gov.ar:1175">
</MediaElement>

Hope it helps!

Upvotes: 1

Related Questions