Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to play music on the background while running a MIDlet

From Wiki
Jump to: navigation, search

This article explains how to play music in your MIDlet, and the effects of running a MIDlet (both with and without music player functionality) while the device music player is running.

Article Metadata
Code ExampleTested with
SDK: Nokia SDK for Java 2.0
Devices(s): Nokia Asha 311
Created: jarmlaht (18 Apr 2013)
Last edited: hamishwillee (30 Jul 2013)



Series 40 devices can often run MIDlets while using the device music player to play audio in the background - although this may slow down MIDlet execution, and in lower end/older devices may not be possible at all! If the MIDlet plays audio the background device music player will stop. Depending on how the MIDlet code is written, the background music may restart when the MIDlet stops playing the file or it may restart only when the MIDlet is closed.

This article shows how to implement audio playback using the Mobile Media API (JSR-135), and how to control whether or not background audio is restarted after the MIDlet player finishes (using MMAPI Player states).

Note.pngNote: Nokia Asha 501 doesn't support this functionality. In that device music player or radio is closed, if MIDlet is launched and it plays music.

How to play an MP3 file

MMAPI uses the Player class and several controls for managing and controlling playback. VolumeControl is used for controlling the playback volume.

The following code shows how to play a local MP3 file:

try {
InputStream is = getClass().getResourceAsStream("/song.mp3");
if (player == null) player = Manager.createPlayer(is, "audio/mp3");
vc = (VolumeControl)player.getControl("VolumeControl");
} catch (IOException ioe) {
// Exception handling
} catch (MediaException me) {
// Exception handling

There is more detail on using the VolumeControl in the article MIDlet volume control in Series 40 and S60 devices.

MMAPI Player life cycle

A Player has five states: UNREALIZED, REALIZED, PREFETCHED, STARTED, CLOSED. These states are defined in the MMAPI specification and they provide programmatic control over operations.

The image below explains the states and the state transition methods of the Player. Understanding the Player states is important for having control of the resources your MIDlet is reserving and also for minimizing the lag time before playback starts.

Player states

How to check for current Player state

Player events can be used for checking the current state of the Player:

// The MIDlet must implement PlayerListener and PlayerListener must be added to the player
public class PlayerTest extends MIDlet implements CommandListener, ItemStateListener, PlayerListener {
public void playerUpdate(Player player, String event, Object eventData) {
if (player.equals(myPlayer)) {
System.out.println("event: " + event);
if (event.equals("volumeChanged")) {
// Do something with the volume change.

How to allow background audio to restart

Background audio will restart if the Player is closed, and all resources are released:

player.close(); // Player closes and goes to CLOSED state.

Background audio will not resume if the Player is "stopped"

try {
player.stop(); // Player stops and it goes to PREFETCHED state.
} catch (MediaException me) {

Verifying background player behaviour when running a MIDlet

The PlayerTest MIDlet can be used for demonstrating how background music is affected by the player state. The MIDlet plays back an MP3 file included in the MIDlet JAR file. There are commands for stopping the playback (Player goes to PREFETCHED state) or closing the Player (Player goes to CLOSED state).

The stopping behaviour can be tested using the following steps.

  1. Start the music player first and start playing a music file.
  2. Put the music player to background.
  3. Start the PlayerTest MIDlet and select Play command to start MIDlet audio playback.
  4. When the music starts, the music player stops its playback.

The restart behaviour can be tested using either of the following steps.

  • Select Stop in the MIDlet. Music playback is stopped (Player goes to PREFETCHED state). The background music player does NOT continue playback.
  • Select Close in the MIDlet. Music playback is stopped (Player goes to CLOSED state) and the background music player continues playback.

The image below shows, how the Player states are shown in the MIDlet UI:

Player states in the MIDlet UI

Example application containing sources, PlayerTest.jad and PlayerTest.jar

This page was last modified on 30 July 2013, at 03:46.
55 page views in the last 30 days.