The current application certification requirements for Windows Phone define, in chapter 6.5, special criteria for applications which need to interrupt the ongoing audio playback (active music players or radio). This applies mainly to games when their background music track is intended to replace whatever is currently playing on the device.
The rules make sense and can be easily implemented, but the trick is in making sure that any currently playing track can be paused/restored, regardless of the source. The code snippet below shows how the Pause/Resume functionality can be implemented, although as you can see, the solution cannot guarantee the restore functionality.
First you need to add the namespaces for XNA and Radio to your code:
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Media; using Microsoft.Devices.Radio; |
And of course the corresponding references to your project. Then in your class, just before playing/stopping your own audio playback, call the Pause and Resume methods implemented similar to the ones listed below:
bool radioIsPaused = false;
private void Pause(object sender, RoutedEventArgs e)
{
// stop the FM Radio if playing on Windows Phone 7
try
{
FMRadio radio = Microsoft.Devices.Radio.FMRadio.Instance;
if (radio.PowerMode == RadioPowerMode.On)
{
radio.PowerMode = RadioPowerMode.Off;
radioIsPaused = true;
}
}
catch (RadioDisabledException)
{
// radio not supported, move along
}
if(!radioIsPaused)
{
// Regular FrameworkDispatcher.Update calls are necessary for fire and forget
// sound effects and framework events to function correctly.
// See http://go.microsoft.com/fwlink/?LinkId=193853 for details.
FrameworkDispatcher.Update();
// This should stop any 3rd party player
// May briefly resume a paused XBox Music player
MediaPlayer.Resume();
// This will stop the XBox Music player
MediaPlayer.Pause();
}
}
|
Note that MediaPlayer can only control the XBox Music player but attempting to resume playback on it will pre-empt the other active players (tested with Nokia Music and Podcatcher). If the XBox Music app is running in the background but the playback is already paused, the Resume call will activate it briefly before the subsequent Pause call sends it back to sleep.
For controlling the radio we use the FM Radio API being careful to handle the exception thrown on the smartphone models which do not support the radio feature.
Once we are ready to return the audio channel to the paused players:
private void Resume(object sender, RoutedEventArgs e)
{
if (radioIsPaused)
{
try
{
FMRadio radio = Microsoft.Devices.Radio.FMRadio.Instance;
if (radio.PowerMode == RadioPowerMode.Off)
{
radio.PowerMode = RadioPowerMode.On;
radioIsPaused = false;
}
}
catch (RadioDisabledException)
{
#if DEBUG
// radio not supported, how was it paused?
throw;
#endif
}
}
else
{
// This will resume the Xbox player
MediaPlayer.Resume();
// We cannot resume Nokia Music or 3rd party players
}
}
|
With this code and a couple of UI elements to satisfy the rest of the specific requirements you should be able to pass the certification testing without problems. Good luck!



