Playing Audio with J2ME

。_饼干妹妹 提交于 2019-11-26 21:24:26

问题


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?


回答1:


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;



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/473809/playing-audio-with-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!