简体   繁体   English

J2ME背景音乐

[英]J2ME background music

I would like to play background music in my J2ME game. 我想在我的J2ME游戏中播放背景音乐。 How can I code that function? 我该如何编写该功能?

You have a similar question at Playing Audio with J2ME 您在使用J2ME播放音频时遇到了类似的问题

For ease I have posted some code from that thread: 为方便起见,我发布了该线程的一些代码:

// 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; 

In reference to QuickRecipes comment Chan, MIDIs are preferable to use for background music since they are much smaller than WAVs and the primary disadvantage of MIDIs, a possibly delay start time is less of an issue when playing background music than it would be for sound effects such as gunshots that need to play instantly. 在参考QuickRecipes评论Chan时,MIDI更适合用于背景音乐,因为它们比WAV小得多并且是MIDI的主要缺点,播放背景音乐时可能延迟的开始时间不是音效的问题。比如需要立即发挥的枪声。 As far as code changes MIDIs and WAVs are just different encoded types, a generic Player can play either type so the code changes are essentially nill. 就代码更改而言,MIDI和WAV只是不同的编码类型,通用播放器可以播放任何类型,因此代码更改基本上是nill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM