Over the past few days, I have been using the moto SDK to develop a Java game for the moto e680i. I have posted some experiences using blog:
It is relatively simple to play a sound file in the Java program of the moto series mobile phones, but there is a problem when it is used for interactive sound effects.
According to some data from Moto, the following restrictions apply to playing sounds on the moto mobile phone:
Apart from playing one Midi and one WAV at the same time, the moto mobile phone cannot play multiple sounds at the same time, and it must first play the Midi and then play the wav;
In addition to the midi or scale sequence, multiple instances of the player cannot enter the prefetched state (pre-read audio streams) at the same time );
Before playing a new sound, the previous player must stop and release resources (stop (); deallocate ()).
If you encounter the following problems, try my solution:
1) only play once or several times, and then the sound cannot be made;
2) When playing a new sound, the previous sound is always played again;
3) or other strange problems.
My code:
// Reference the following
Import javax. microedition. Media. player;
Import javax. microedition. Media. playerlistener;
// Define multiple types of sound effects and sound files
Private string [] arrsoundfile = {"start. Mid", "right.wav", "wrong.wav", "help.wav", "end.wav "};
Private string [] arrsoundtype = {"audio/X-Midi", "audio/X-WAV ", "audio/X-WAV "};
// Define a player instance
Public player;
// Define a variable to determine whether to generate a voice
Public Boolean issoundopen = false;
// Playing the main audio process. The parameter is the index number of the previously defined sound array.
Public void playmedia (INT Ikey) throws exception {
// Create an instance
Player = manager. createplayer (
Getclass (). getresourceasstream ("/MIDI/" + arrsoundfile [Ikey]), arrsoundtype [Ikey]);
// Bind status update event Process
Player. addplayerlistener (this );
// Play the sound
Player. setloopcount (1 );
Player. prefetch (); // prefetch
Player. Realize (); // realize
Player. Start (); // and start
}
// Player status update event Process
Public void playerupdate (player, string event, object eventdata ){
// Debug the tracking process
// System. Err. println ("Event:" + Event );
// If the playback ends, the player instance is released and disabled.
If (event. Equals ("endofmedia ")){
Player. deallocate ();
Player. Close ();
}
}
// Call the playing sound process
Public void playsound (INT Ikey ){
If (issoundopen)
{
Try {
Playmedia (Ikey );
} Catch (exception e ){
System. Err. println ("unable to play:" + E );
E. printstacktrace ();
}
}
}
/*
Reprinted. Please keep the following copyright description
======================================
R & D and lightning Hall
======================================
If you do not retain the above copyright, it means you are very poor.
*/
You can test it on the moto host.