One. Thread processing [not recommended]:
<1> How to use: Suppose and our .java files are A.MID files in the same directory
1 Declare a SoundEffects global object SND in a .java file, such as:
PRIVATE SOUNDEFFECTS SND = New SoundEffects ();
2 Write the location where the sound is required
New thread () {
Public void run () {
SND.PLAYMIDI ("/ a.mid");
}
} .start ();
// This will play
3 Stop, write the place where you need to stop
IF (SND! = NULL)
SND.DESTROY ();
// This SND is released to null, if you want to play again, first
// SND = New SoundEffects ();
<2> Advantages: You can use multiple SoundEffects objects in an application, you can use multiple Player objects. The program code can be clearly seen from different Player objects to play different sound files. (Maybe this is not an advantage)
<3> Disadvantages: Overhead (occupying CPU Time), open and close the thread has a lot of overhead (here the overhead here should be CPU scheduling overhead), tested, using this thread tension to play, the game application operation 5-6 hours later, "crash" phenomenon will appear.
The SoundEffects source file is shown in the attachment.
two. Non-threaded processing [recommended]:
Reason: Java Although the multi-threaded process is supported, the use of multithreaded has always been a particularly cautious choice, because the use of multi-threaded use is more complicated, and it will occupy a lot of system resources, Java is still on the unija. The application is even more such, so if it is slightly improper or estimated, it is possible to exhaust the resources of the system, which is this reason, so in our application, it is not the use of multi-threaded process, because any The advantages will be meaningless because of the "hanging" in the process of users. Treatment in a single thread is actually very simple, I just simply mention, I believe you will understand. :)
How to use: The sound playback function of the MIDI format in the Unija application does not support multiple Player objects in the same thread, so the background music and sound effects cannot be played simultaneously, but allowing different sounds to be played, the specific method is to play a sound file or During playing a sound, if you want to change another sound, you should turn the current Plyaer to clear the Player object and system.gc () (release Player Resources), then use this Player object to regenerate a new Sound file object, and play. (Note: If this process, if it frequently does it, it will lead to system overhead. Therefore, it is recommended that if you use the voice file using the MID format, it is best not to add more sound effects, one or two, my experience is when applying Menu menu can loop play background music, only sound effects when the application enters the game)
three. Personal summary:
1. Playback Problem: The sound playback function of the MIDI format in the UNIJA application does not support multiple Player objects. After playing a sound file, empty the object (release the Player Resources), can only exist in the application, otherwise it will not Play another sound file.
2. Overhead Problem: It is recommended to use non-threaded processing sound method 3. Cycling question: In the constructor of the SoundEffects class, I declare a parameter time, used to control the number of sound playings, such as: New SoundEffects (10) represents this Player play object will loop to play ten times; in the Player class In, I didn't find the way infinitely loop play, in the SoundEffects class I wrote, I usually set TIME to 1 million times.
four. Attachment: SoundEffects.java (modified, recommended)
Import javax.microedition.media. *;
Import Java.io.InputStream;
Import java.io.bytearrayinputstream;
Import javax.microedition.media.Control. *;
Import java.io. *;
/ ** Sound behavior, this class is based on MIDP2.0 supported sound.
Wave Audio Files: Audio / X-WAV
Au Audio Files: AUDIO / BASIC
MP3 AUDIO FILES: AUDIO / MPEG
MIDI
FILES: AUDIO / MIDI
Tone Sequences: Audio / X-Tone-SEQ
How to use: Create an object first, then call the playback function, remember to call DESTROY () when the program exits, or no longer use the sound function
* /
Public class soundeffects {
String filename = NULL;
String contenttype;
Int Time;
Player Player = NULL;
InputStream IS = NULL;
Public SoundEffects (int Time) {
this.Time = Time;
}
Public void Playwav (String filename) {
THIS.CONTENTTYPE = "AUDIO / X-WAV";
Plays (filename);
}
Public void Playau (String filename) {
THIS.CONTENTTYPE = "Audio / Basic";
Plays (filename);
}
Public void playmp3 (String filename) {
THIS.CONTENTTYPE = "AUDIO / MPEG";
Plays (filename);
}
Public void playmidi (string filename) {
THIS.CONTENTTYPE = "AUDIO / MIDI";
Plays (filename);
}
Protected synchronized void playsound (String filename) {
THIS.CLOSE ();
THIS.FILENAME = filename;
Playfile ();
}
protected void close () {
IF (Player! = null) {
Player.Close ();
Player = NULL;
}
IF (is! = null) {
Try {
Is.close ();
}
Catch (Exception E) {}
IS = NULL;
}
Filename = NULL;
}
Public void destroy () {this.close ();
}
Public void startplay () {
Try {
Player.start ();
} catch (MediaException E) {
E.PrintStackTrace ();
}
}
Public void facuseplay () {
Try {
Player.stop ();
} catch (MediaException E) {
E.PrintStackTrace ();
}
}
Public int getState () {
Return Player.getState ();
}
Public void playfile () {
Player = NULL;
IF (filename! = NULL) {
Try {
IS = getClass (). getResourceAsStream (filename);
Player = Manager.createPlayer (IS, ContentType);
//p.prefetch ();
Player.Realize ();
VoluMecontrol VC = (VoluMecontrol) Player.getControl ("VolumeControl");
vc.setlevel (100);
Player.SetLoopCount (Time);
Player.start ();
}
Catch (IOException E) {
E.PrintStackTrace ();
}
Catch (javax.microedition.media.mediaexception e) {
E.PrintStackTrace ();
}
}
}
}