[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
J2MEFAQ_HowDoIDoSound

J2MEFAQ How Do I Do Sound

Sound is not as mature in MIDP1.0 as MIDP2.0 so we will cover how to code sound in both versions.

MIDP1.0 Sound

MIDP1.0 is a mix of J2ME compliant sound APIs and closed source APIs that are vendor specific to specific devices series. Lets look at alert sounds within the MIDP1.0 APIs.

There are 5 types of alerts; Information, Confirmation, Warning, Alarm and Error. The AlertType class does not have any public constructors so one would do a sound by:

AlertType.INFO.playSound(Display.getDisplay(midlet));
AlertType.ALARM.playSound(Display.getDisplay(midlet));
AlertType.ERROR.playSound(Display.getDisplay(midlet));


There are also device specific extensions like Nokia's UI API which can be sed to play sounds in MIDP/1.0. Some MIDP/1.0 devices support the Mobile Media API as well, e.g. the Nokia/3650 phone.

MIDP2.0 Sound

In MIDP2.0 one is using the Mobile Media API. Notes have a int value of 0 to 127, for example:

int note = ToneControl.C4;
Manager.playTone(note, 300, 50);


plays the middle C note for 300 milliseconds at middle volume of 50 out of 100. Generally with any media even sound you will of course want to wrap the call to play a sound with exception catching code.

This is not the only way to play sound within MIDP2.0 as we can also play wav/mp3 sounds and create a synthesizer. For example, we can play a wav or mp2 sound by:

Player p;
VolumeControl vc;
try {
   p = Manager.createPlayer("http://server/somemusic.mp3");
   p.realize();
   // get volume control for player and set volume to max
   vc = (VolumeControl) p.getControl("VolumeControl");
   if(vc != null) {
      vc.setVolume(100);
   }
   // the player can start with the smallest latency
   p.prefetch();
   // non-blocking start
   p.start();
} 
catch(IOException ioe) {
} 
catch(MediaException e) {
}


This is of course not the only way to deliver wav/pm3 sound as you can also use the jar itself or RMS to store the sound clip as in this example:

RecordStore store;
int id;
// play back from a record store
try {
   InputStream is = new ByteArrayInputStream
     (store.getRecord(id));
   Player player = Manager.createPlayer(is, "audio/X-wav");
   p.start();
} 
catch (IOException ioe) {
} 
catch (MediaException me) {
}


Or delivering the sound from the jar as:
try {
   InputStream is = 
     getClass().getResourceAsStream("audio.wav");
   Player player = Manager.createPlayer(is, "audio/X-wav");
   p.start();
} 
catch(IOException ioe) {
} 
catch(MediaException me) {
}


Other Profiles

In other profile such as the ones in use with iTV/JavaTV(iTV and Telematics) one uses the JMF library. The only difference between JMF and the Mobile MultiMedia API in MIDP2.0 is what Manger class to use and the methods. The coding principles of creating a player first and then loading the media is the saem in boht JMF and Mobile Media API.

last edited (June 22, 2004) by enough, Number of views: 7714, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.