[Article] Author: Ma Yan build time: 2004-10-27 Source: NEW YORK Editor: Ark
[Documentation] With the MIDP 2.0 Media API, you can add sound effects for your games and other applications, vocalize, play music, and other different audio, you can use the MIDP 2.0 Media API, you can The games and other applications increase sound effects, vocalize, play music, and other different audio on the MIDP device. Introduction is almost 1.0 for MIDP (Mobile Information Device Profile), based on its simple video player (third-party manufacturer development) is also released. This player only supports MPEG format, and it is not complicated, but it shows the ability to watch and use media (this example is video), which greatly stimulates the interest of programmers. However, this early video player is not designed for programmer, so if you want to use the function of playing video in the software, you must copy all code (this may have a doubt that may have a doubt) or write all the relevant code. This shows that the demand for the API playing various media is obviously existing, but only for playing video development APIs do not implement all the purposes of the API. As the mobile media API (MMAPI) expert committee began to confirm the concept of moving media, they first need to provide reference from simple cellular phones to PDAs and tablet-based telephones. In the end they completed two different API settings: • Mobile Media API (MMAPI) designed for mobile devices with advanced sound and multimedia performance. · Media API (MIDP2.0), suitable for limited mobile devices (only audio only). After the above development, it is strange that the first version of the API does not seem to support video, but there is a good explanation for this: Some MIDP devices are too limited. In fact, the need for MIDP2.0 Media API is easy to hook, it must provide support for the following features: • Control, "Start Play", "Start Play", "Stop", "Suspension", etc. The basic control · Media special effect control, For example, volume, content query MIDP2.0 Media API gives you created, playing a simple melody (only syllable) and audio. It is mentioned that the sound first enters the ringtone of our mind, but in fact, all the relevant concepts involved in the API and MIDP have the same restriction: it can only be used in the Java environment, so you can't create new ringtones for your mobile phone. But except for other games or applications that need more complex sound than ringtones, the API provides huge benefits. Universal Architecture API consists of three parts: Manager, Player and Control, and two packages: javax.microedition.media, javax.microedition.media.control. The MedIA package contains Manager and possible Players, and the Control package contains all Controls. The most important classes in the API are Manager. Manager's use includes, create a variety of different types of Players (for playing audio, video, etc.) to obtain a variety of support protocols and content formats, and play simple tones. Player class is used to play multimedia content in various formats. Control is an interface to control Players, control, such as volume, tone, and tone. Figure 1 Manager, Player, Control Estate Inter-Cymbositionary
Creating a player is similar to creating a network connection, depending on the situation, you need to call one or both of the Manager in the Manager. The following code creates a Player based on the audio file on the Internet, then playing the audio: try {player audioplayer = manager.createplayer ("http://www.nullplace.com/music.wav"); audioplayer.start (); } Catch (IOException IOE) {} Catch (MediaException Me) {} The second example is created from the application .jar file creates a Player, and then play audio:
try {InputStream is = getClass () getResourceAsStream ( "music.wav");. Player audioPlayer = Manager.createPlayer (is, "audio / X-wav"); audioPlayer.start ();} catch (IOException ioe) {} Catch (MediaException Me) {} issues a simple tone to issue a simple tone is a simple process, and can be easily implemented with a handful method in the Manager class. Note that the following MIDlet has two simple actions: one is an exit application and the other is a simple tone. A tone is implemented by a SimpleTone () method, and it is actually implemented by the Manager class's Playtone (int Note, Int Duration, int Volume) method.
import javax.microedition.midlet *;. import javax.microedition.lcdui *;. import javax.microedition.media *;. import javax.microedition.media.control *;. public class MediaMIDlet extends MIDlet implements CommandListener {private Player mp; private Display display; private List list; private Command exitCommand = new Command ( "Exit", Command.EXIT, 2); private Command playCommand = new Command ( "Play", Command.ITEM, 1); public MediaMIDlet () {display = Display.getDisplay (this); list = new list ("demo", list.implicit);} public void startapp () {list.addcommand; list.command (playcommand); list.commandlistener (THIS); display.setCurrent (list);} public void destroyApp (boolean unconditional) {} public void pauseApp () {} public void commandAction (Command c, Displayable s) {if (c == exitCommand) {destroyApp (false); notifyDestroyed ( );}}}} Private void simpletone ()}} private void simpletone ()}, {manager.playtone (ToneControl.c4, 100, 80);} Catch (Exception EX) {}}} if notes Not a middle sound C, set the sound The tone in the sound requires some techniques. As shown above, in fact, the constant attribute in the ToneControl class is indicated by the sport C: ToneControl.c4. There is also a special constant indicating that no sound: ToneControl.silence. The next section describes how to use these ready-made code architecture to create a tune. Playback Tune The following code describes the variables of different notes (Bytes) expression, where is the most interesting thing is to create an array mysequence: 1. Array elements are integer variables, pair of constant values or musical notes. 2. Version and Tempo are set, followed by blocks 0 and 1 (A). 3. Play part. After the tune is ready, the rest of the code shows how to create a ToneControl class and how to play tune with it.