Player pooling

Player pooling is an efficient way to store REALIZED and PREFETCHED state Player objects. In general, the start of pooled Players should be a bit faster than the unprepared players, because the realization and prefetching of Players takes time. A pooled player can be accessed several times just by calling the Player's start() method. This should minimize the delay in starting the media. Currently, Player pooling can be implemented in S60 devices. At least in Series 40 2nd Edition and earlier devices, multiple realized and prefetched players cannot be pooled because only one Player object can be set up at a time. Although some Series 40 devices might allow to realize multiple players, multiple-player prefetching is not supported.

In the below code sample with code comments, a Vector is used as a Player pool. However, any other suitable storage could also be used (for example, array).

Vector vect = new Vector(); // Vector to store Players
String file = ...  // Give the path of the media file here
String type = ...  // Give the media type of the file here
  1. An InputStream needs to be obtained for the media file:

    // Create first player...
    InputStream is = getClass().getResourceAsStream(file);
  2. Create the Player with the Manager using the InputStream and the media type (MIME; for example, audio/midi for MIDI audio) of the file:

    Player player1 = Manager.createPlayer(is, type);

  3. You may optionally make your class listen for the player events:

    player1.addPlayerListener(this);

    When doing this, instruct your class to implement the PlayerListener interface and add the required playerUpdate method. Now the player can be prefetched and realized by calling the realize() and prefetch() methods:

    player1.realize();
    player1.prefetch();

When the players are realized and prefetched, they are ready to play. This can be seen from the figure in section Player State Model, which describes the states of a player. When creating other players, just repeat the previous steps. Finally, after you have created all the needed players, store them in the pooling place, which is in this case a Vector object:

vect.addElement(player1); 
vect.addElement(player2);

When you need to access the player, just obtain the reference to the player object from the pool. The following code lines show how to get a player from the vector pool and start playing the sound:

// Play the second player
Player player2 = ( Player )vect.elementAt(1);
player2.start();

Note: Do not close the player if you are going to use it later. If the player has been closed, it needs to be reinitialized before reuse. However, if the player is not needed any longer, close it and remove it from the pool to let garbage collector clean the memory.