使用J2ME Media库的音频播放的最佳方法是什么?例如,我应该使用MMAPI还是应该使用Midlet的platformRequest(String s)方法?
以下代码适用于支持JSR-135的90-95%的手机.对所有各种方法调用进行排序是可移植的关键.这适用于JAR的本地声音.任何流式音频都将是另一个问题:)
// 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;