Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play background music online in Windows 8

Tags:

c#

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!

like image 742
thongaduka Avatar asked Jul 05 '12 09:07

thongaduka


People also ask

How can I add background music to an audio file online?

Adding background music to recorded audio Select a music category, then search or scroll to find and preview the background music you like for your segment. To add it to your recording, finish by clicking the 'checkmark' icon while hovering over the desired music track.

How do I add music to a video on Windows 8?

Right click a video file > Open with > Photos. On the toolbar, click Edit & Create > Create a video with text. You are now in Photo's Editor Screen. Click on Music and select an audio file from Music folder.


2 Answers

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.

like image 129
akton Avatar answered Oct 05 '22 11:10

akton


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!

like image 26
Armando Avatar answered Oct 05 '22 11:10

Armando