使用J2ME播放音频

时间:2009-01-23 17:38:46

标签: audio java-me media-player mmapi

使用J2ME Media库播放音频的最佳方法是什么? 例如,我应该使用MMAPI还是应该使用Midlet的平台Request(String s)方法?

4 个答案:

答案 0 :(得分:7)

以下代码适用于支持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;

答案 1 :(得分:3)

best ,你的意思是什么?最好的质量?最佳用户体验?大多数符合标准?

基本上(这不是我所知道的最好的答案),它取决于平台 - 这是J2ME熟悉的主题 - 因为实现可以有很大差异,MMAPI通常利用一些本地媒体播放器软件/硬件。在黑莓手机上,MMAPI运行良好,有一些怪癖。

我的建议是使用MMAPI,它运行良好 - 它将为您提供最便携的应用程序。试验或浏览开发板,了解您的特定目标设备,了解它在您想要支持的手机上的功能。

答案 2 :(得分:0)

在某些使用平台请求的设备上会导致您的应用程序关闭,而在其他设备上某些平台请求会使设备崩溃(某些设备上的HTTP URL以外的任何其他设备)。

因此,您可能需要使用这两种方法,根据设备测试选择使用哪种方法。

答案 3 :(得分:-1)

本地音频(在Jar文件中)更容易支持,如上面Shane的回复中所述。对于网络上的内容,由于诸如服务器连接问题,手机内存等许多因素发挥作用而变得更加困难。如果您需要支持大量手机,那么建议您使用2-3种实现技术并进行相应的使用。