J2ME背景音乐

问题描述:

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

你必须在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

如果这是用于游戏中的背景音乐,我还建议使用MIDI音频数据,而不是简单的.wav文件。 – 2010-01-12 11:35:33

+0

@ QuickRecipesOnSymbianOS:你可以更具体,给我一些代码示例? – 2010-01-13 01:40:20

在参考QuickRecipes评赞,MIDIS是可取的,因为它们使用的背景音乐比WAV小很多,也是MIDI的主要缺点,播放背景音乐可能会延迟启动时间,而不是需要立即播放的枪声等声音效果。就代码更改而言,MIDI和WAV只是不同的编码类型,通用播放器可以播放任何一种类型,因此代码更改基本上是无效的。