Perfect solution for playing audio files of Java programs in Moto e680i

Source: Internet
Author: User
Tags prefetch

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.