Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

java me - Playing Audio with J2ME

What is the best way to play audio utilzing the J2ME Media libary? For example, should I make use of the MMAPI or should I just use the Midlet's platformRequest(String s) method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code should work for 90-95% of handsets out there that support JSR-135. Ordering of all the various method calls is key for this to be portable. This is for sounds local to your JAR. Any streamed audio would be another problem altogether :)

// loads the InputStream for the sound
InputStream inputStream = this.getClass().getResourceAsStream( musicFile );

// create the standard Player
musicPlayer = Manager.createPlayer( inputStream, musicEncoding );
musicPlayer.prefetch();

// add player listener to access sound events
musicPlayer.addPlayerListener( this );

if( loopMusic )
{    
    // use the loop count method for infinite looping
    musicPlayer.setLoopCount( -1 );
}

// The set occurs twice to prevent sound spikes at the very 
// beginning of the sound.
VolumeControl volumeControl = 
   (VolumeControl) musicPlayer.getControl( "VolumeControl" );
volumeControl.setLevel( curVolume );

// finally start the piece of music
musicPlayer.start();

// set the volume once more
volumeControl = (VolumeControl) musicPlayer.getControl( "VolumeControl" );
volumeControl.setLevel( curVolume );

// finally, delete the input stream to save on resources
inputStream.close();
inputStream = null;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...