views:

2908

answers:

5

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?

+2  A: 

By best, what do you mean? Highest quality? Best user experience? Most standards-compliant?

Basically (this isn't the best kind of answer I know), it depends on the platform - something that's a familiar theme with J2ME - since implementations can vary widely and the MMAPI usually leverages some native media player software/hardware. On BlackBerry the MMAPI works great, with a few quirks.

My advice is to use the MMAPI where it works well - it'll give you the most portable application. Experiment or browse the developer boards for your specific target device set to see how well it functions on the handsets you want to support.

Anthony Rizk
+3  A: 

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;
Shane Breatnach
A: 

On some devices using a platform request will cause your application to close and on others certain platform requests will crash the device (anything other than HTTP URLs on some devices).

So you may need to use both approaches, choosing which one to use based on device testing.

Kevin ORourke
A: 

Local audio (in Jar file) is easier to support as explained in Shane's reply above. For content over the web it becomes much harder as a lot of factors like server connectivity issues, handsets memory etc come into play. If you will be required to support a large number of handsets then its advisable to work on 2-3 implementation techniques and use accordingly.

omermuhammed
A: 

How to obtain "musicEncoding" in the below line of code?

musicPlayer = Manager.createPlayer( inputStream, musicEncoding );

Mio
That's a different question, not an answer to the one covered in this thread. You should better post it as a new question, more people will see it that way.
sth