用 J2ME 播放音频 [英] Playing Audio with J2ME

查看:20
本文介绍了用 J2ME 播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 J2ME 媒体库播放音频的最佳方式是什么?例如,我应该使用 MMAPI 还是应该使用 Midlet 的 platformRequest(String s) 方法?

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?

推荐答案

以下代码应该适用于 90-95% 的支持 JSR-135 的手机.所有各种方法调用的排序是可移植的关键.这是用于 JAR 本地声音的.任何流式音频都将是另一个问题:)

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;

这篇关于用 J2ME 播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆