Pause/resume active music players

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!

Quick start in game development for Series 40 full touch

Series 40 full touch and Nokia SDK for java 2.0 introduce great improvement in application capability for low-price range devices. There are enough materials that explain new things as 3D, sensors, touch UI, etc… Mostly the documents are for experienced developers hungry for new application features. What about newbies in Series 40?  We are going to fill the gap with the wiki article series “Quick start in game development for Series 40”.

Why games? I think nowadays for individuals it is hard to compete with business applications on the market by several reason. One of the reasons is – devices are sold with preinstalled good application collection for every-day users’ needs. There is no much room for improvement for third-party developers. Not the same situation is in game world.  Express yourself! Game development is more art than business app development.  Game development requires more than just programming and UI design skills. It might start from making a story, making characters and end-up living creatures in your world. That is rather exiting opportunity. Considering growing low-price device market available by Nokia you can get even some profit for your game or at least recognition. Target your game to Nokia Store!

So, how do I start? Please go to the wiki and follow the step-by-step instructions.  So far there are two articles in the series starting from creating game application skeleton to adding touch support in it. I hope to keep my pace and release more articles in near future on regular basis. What will be next in the series:

  • Path-finding and collision detection algorithms
  • Main menu implementation
  • Supporting devices with different capabilities. That is actual question for SVG graphics and screen geometry because touch screen supporting is not conflicting in code implementation with key-only devices as Nokia 110.
  • Advertising your game with social networks, like sending game scores via Facebook  (honestly I have only vague idea how to implement it. Let’s see how it goes. But definitely that feature is in great demand)

Caution!  The articles are focused on technical problems only – bring there your imagination and art :)

Please comment.

Regards,
Igor

 

Introduction to Series 40 full touch for developers

Car Race 3D Exercise App running on the Nokia Asha 311We’re kicking of the webinar series for the new Series 40 Touch phones tomorrow! The first session will walk you through the new Java ME APIs introduced in the Developer Platform 2.0.

The packed agenda covers all the important topics that you will need to know about when you’re moving your Java ME apps to the new touch UI:

  •  Introduction
    • Platforms & Versions
  • New features for developers
    • UI
    • LWUIT
    • Text Input
    • Touch Input
    • Sensors
    • Location & Maps
  • App Compatibility
  • Publishing & Monetization
  • Resources
     

The recording of the first webinar session is online for viewing. The downloadable version of the slides is slightly extended, featuring some more details than in the 1h webinar. 

You can download the full source code of all the apps shown during the presentation – the blue text box in the top right corner or the slide always refers to the full example being demonstrated.

JavaME-Touch-Examples-v2.0.0.zip

As a special bonus, the example package and the downloadable slides also include a few exercises (+ the corresponding solutions), so that you can put your new knowledge about the APIs into use right away! The excercises are both based on 3D graphics:

  • In Monkey Touch 3D you’ll add drag & pinch support to view the 3D model of a monkey, and add orientation support to dynamically switch the app between landscape & portrait mode, depending on the physical orientation of the phone.
  • Car Race 3D let’s you extend an endless 3D racing game with support for controlling the car using acceleration sensors, instead of physical keys (which would no longer be availble on the new touch phones)
     

Registration

In different webinars during the following days we’ll also look at the design aspect and the IDEs (both the new Nokia IDE for Java ME as well as NetBeans). Every session is held two times, so that you can join no matter which time zone you live in.

Register for the webinars now!

Developer Library latest update

This Developer Library and API reference update includes documentation for PR 1.2 features and documentation enhancements for existing features:

As always, your feedback about the Developer Library is very welcome!

 

 

Developer Library latest update

This Developer Library and API reference update includes documentation for PR 1.2 features and documentation enhancements for existing features:

As always, your feedback about the Developer Library is very welcome!