J2ME背景音乐

时间:2010-01-12 01:56:27

标签: java java-me

我想在我的J2ME游戏中播放背景音乐。 我该如何编写该功能?

2 个答案:

答案 0 :(得分:4)

您在Playing Audio with J2ME

上有类似的问题

为方便起见,我发布了该主题的一些代码:

// 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 :(得分:0)

参考QuickRecipes评论Chan,MIDI更适合用于背景音乐,因为它们比WAV小得多,并且是MIDI的主要缺点,播放背景音乐时可能延迟开始时间不是问题。对于需要立即播放的枪声等声音效果。就代码更改而言,MIDI和WAV只是不同的编码类型,通用播放器可以播放任何类型,因此代码更改基本上是nill。