TechBuzzPro

Detect Background Music on Windows Phone: How-To

· 5 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can detect whether music is playing in the background on a Windows Phone by using the MediaPlayer class from the Microsoft.Xna.Framework.Media namespace in a Silverlight or WinRT app. The property MediaPlayer.GameHasControl tells you if your app currently has control of the media queue; if it returns false, another app (or the system) is playing music in the background. This guide walks you through the steps to check that state, handle it correctly, and avoid common pitfalls. It applies to Windows Phone 8.1 and earlier Silverlight-based apps; Windows 10 Mobile uses the Windows.Media.Playback APIs instead, which are not covered here.

What You'll Need

  • A Windows Phone project (Silverlight or WinRT) opened in Visual Studio.
  • Reference to Microsoft.Xna.Framework (usually added automatically for Silverlight projects).
  • Basic familiarity with C# and event handlers.
  • A physical device or emulator running Windows Phone 8.1 or earlier for testing.

Step-by-Step

  1. Add the required using directive. At the top of your code file, add using Microsoft.Xna.Framework.Media;. This gives you access to the MediaPlayer class.
  2. Check if your app has media control. Call MediaPlayer.GameHasControl. It returns true if your app is the active media source; false means another app or the system is playing audio in the background. Example: bool isBackgroundMusicPlaying = !MediaPlayer.GameHasControl;
  3. Handle the state change event. Subscribe to MediaPlayer.ActiveSongChanged to react when the active song changes. In the handler, re-check GameHasControl to update your UI or pause your own audio. Note that this event fires when the active song changes, not necessarily when playback starts or stops.
  4. Query the media queue state. Use MediaPlayer.Queue and MediaPlayer.State to get more detail. MediaPlayer.State returns a MediaState value: Playing, Paused, or Stopped. Combine this with GameHasControl to distinguish between your app's playback and background playback. For example, if GameHasControl is false and MediaPlayer.State is Playing, background music is playing.
  5. Update your UI accordingly. On the UI thread, use the values from steps 2–4 to enable or disable controls, show a message like "Music is playing in another app," or pause your own background audio. Always marshal UI updates using Dispatcher.BeginInvoke if you are on a background thread.
  6. Test with a real background app. Deploy your app to a device, start music in the built-in Music app, then switch to your app. Verify that GameHasControl returns false and that your UI reflects the state. Also test pausing and resuming the background music to see how your app reacts.
  7. Clean up event handlers. In your page's OnNavigatedFrom method, unsubscribe from ActiveSongChanged to avoid memory leaks and unexpected callbacks when the page is not active.

Common Mistakes

  • Assuming GameHasControl is always reliable. It reflects whether your app has control of the media queue, not a definitive "music is playing" flag. Combine it with MediaPlayer.State for accuracy.
  • Forgetting to check on the UI thread. Accessing MediaPlayer properties from a background thread can cause exceptions or stale values. Always read them on the UI thread or marshal the results.
  • Not handling the ActiveSongChanged event. If you only check once at startup, you will miss changes when the user starts or stops music in another app.
  • Using the wrong namespace. The MediaPlayer class lives in Microsoft.Xna.Framework.Media, not System.Windows.Media. Mixing them up leads to compile errors.
  • Testing only on the emulator. The emulator may not accurately simulate background audio from other apps. Always verify on a physical device.

Tips

  • Use MediaPlayer.GameHasControl in combination with MediaPlayer.State to avoid false positives when your own app is paused.
  • If your app plays its own background music, call MediaPlayer.Pause() when GameHasControl becomes false to avoid audio mixing conflicts.
  • Cache the last known state and only update your UI when it changes, to reduce unnecessary redraws.
  • Document that this API is specific to Windows Phone 8.1 and earlier; for Windows 10 Mobile, use the Windows.Media.Playback namespace instead (not covered here).

Troubleshooting

If GameHasControl always returns true even when music is playing in another app, check that your app is not itself holding media control (for example, by having an active SongCollection). Also ensure you are testing on a device where background audio is allowed. If the ActiveSongChanged event does not fire, verify that you subscribed after the media player was initialized and that the event is not being unsubscribed prematurely. If you get a UnauthorizedAccessException when accessing media, confirm that the app has the ID_CAP_MEDIALIB_AUDIO capability enabled in WMAppManifest.xml for Silverlight apps. For WinRT apps, ensure the Music Library capability is declared. If the state seems stuck, restart the device and test again; sometimes the media subsystem needs a fresh start.

Frequently Asked Questions

Does GameHasControl work on Windows 10 Mobile?

No. GameHasControl is part of the XNA/Silverlight media stack used on Windows Phone 8.1 and earlier. Windows 10 Mobile uses the Windows.Media.Playback APIs, which have a different model for detecting background audio.

Can I detect background music without referencing XNA?

On Silverlight for Windows Phone, the MediaPlayer class is the standard way. There is no built-in alternative that provides the same GameHasControl property. You could use the MediaPlayer from Microsoft.Xna.Framework.Media even in non-game apps.

Why does MediaPlayer.State return Stopped when music is playing?

MediaPlayer.State reflects the state of the media queue that your app controls. If another app is playing music and your app does not have control, the state may not reflect the background app's playback. Use GameHasControl to determine if another app is playing.

Is there a way to know which app is playing music?

No. The Windows Phone API does not expose which app is playing background music. You can only detect that some other app has control of the media queue.

What happens if the user pauses background music?

When the user pauses background music, GameHasControl may still return false until the other app releases control. The ActiveSongChanged event may not fire on pause. Check MediaPlayer.State to see if playback is paused or stopped.

Do I need to request any capabilities?

For Silverlight apps, add the ID_CAP_MEDIALIB_AUDIO capability in WMAppManifest.xml to access the media library. For WinRT apps, declare the Music Library capability. Without these, media queries may fail.