MMAPI/Sound Information

From J2me wiki

Table of contents

Sound Formats

With the MMAPI all phones support MIDI of some degree - generally single channel, but not all support all notes. However, you will likely find that if they don't, they can still play your MIDI sound but will substitute it for a similar sound (or perhaps not so similar, depending on your luck).

Other supported formats (depending on the device) include: AMR, WAV (usually only mono, 8-Bit, 8KHz) and MP3.

Playing a Sound

Playing one single sound is relatively easy - create a Player, follow the APIs and call the appropriate methods and Bob's your Uncle, you have a sound playing. Though the best way to play a MIDI tune during a game play, is to create the player from scratch each time before playing it with start(), and then stopping, deallocating and nulling the player each time the game is over - even though you know you're going to use the player again. This is an "easy" way of making the sound work on most possible devices, and it's fairly acceptable because it's only one single MIDI tune. The tricky part is when you have multiple sounds that you want to play, especially close together.

The problem is that phones will only allow you to play one sound at once (some phones limit this to one sound of a particular type at once). In order to play the next sound you must close down all other Players (deallocating them on some phones, but not others) before you can even attempt to play a new sound.

As I mentioned, on some phones you must deallocate the previous Player before you can play a new sound, however on some phones if you deallocate a Player you can't use it again. Note that no matter how hard you try you will always notice a slight delay in playing a new sound if you have to reallocate the Player when you play it.

Device Specific

  • All phones
    • Never try to play two sounds at once - this will break it. This does not apply to newer SonyEricsson phones (phones newer than and including V800).
    • If you need to loop music, write your own playerUpdate method for best compability instead of using setLoopCount()
    • If you stop and deallocate sound - wait some time (~50ms) to release resources before make and start new player.
  • Nokias
    • For ALL Nokias - You can only have a Maximum of 8 Players.
    • For ALL Nokias - You must never deallocate() any Players as this will completely screw it all up.
    • For ALL Nokias - using the stop() and getState() functions is fine.
    • For NGage, 3650, 3620, 36xx series - when creating your Players, you must realize() and prefetch() all of them.
    • For NGage, 36xx series (and possibly more) - occaisionally when the player has finished it will not reset back to the STOPPED state. The only way round this is to make a note of the time you start the sound and then check after x amount of seconds to see if it is still playing, if so, stop() it.
  • Motorolas (v300, v400, v500, v525, v600) and Alcatel735i
    • Do not realize() or prefetch() any Players.
    • Before playing a new sound, you must stop() and deallocate() all other Players (or at least make sure they have been deallocated).
    • Also before playing a sound, you must realize() and prefetch() the Player. (gg this conflicts with the first bullet point.)
    • [Motorolas] You can't use sound and vibration at the same time.
  • Motorola A780
    • Only one MIDIPlayer can be realized
  • Sony Ericsson
    • For ALL Sony Ericssons - stop() makes the tune rewind. It's not supposed to, but it does. This only applies to xmf or midi sounds. AMR, wave, mp3 etc. and most other soundformats works as expected (phones newer than and including V800).
    • For W800 - placing a setMediaTime(-1) after start() messes up the player. Place it before start()
    • For W800 - placing a setMediaTime(-1) immediatly before start() causes the player to only play a splitsecond of the tune
    • T610/T630/Z600 - deallocate() kills the player, can't be prefetched and played again.
    • V800, W800 and similar phones have the capability to play one miditune and one other sound (amr, wave or adpcm) simultaneously.
    • K800 and newer phones can play one miditune and three other sounds (amr, wave or adpcm)simultaneously.
    • Phones which supports the midicontrol can use the midicontrol simultaneously with a miditune (for fx etc.).
  • Siemens
    • For ALL Siemens - setMediaTime(-1) makes the Player start the music.
  • LG
    • For some LG phones - setMediaTime(-1) makes the player start the music. (e.g. LG F2300, but not S5200)


Tip for Nokia S40

Some Nokia S40 phones don't recognise "audio/x-wav". Here is a trick to get around the problem:

InputStream is = getClass().getResourceAsStream( "/noise.wav" );

try {

    noise = Manager.createPlayer( is, "audio/x-wav" );

} catch ( MediaException pe ) {

    noise = Manager.createPlayer( is, "audio/wav" );

}

Useful Resources

http://wireless.java.sun.com/midp/articles/mmapioverview/
http://www.j2meforums.com/forum/index.php?topic=10928.0
http://www.j2meforums.com/forum/index.php?board=2;action=display;threadid=7598
http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/java/p_tips_java_1204.jsp
http://www.j2meforums.com/forum/index.php?topic=12492.0