簡體   English   中英

J2ME背景音樂

[英]J2ME background music

我想在我的J2ME游戲中播放背景音樂。 我該如何編寫該功能?

您在使用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; 

在參考QuickRecipes評論Chan時,MIDI更適合用於背景音樂,因為它們比WAV小得多並且是MIDI的主要缺點,播放背景音樂時可能延遲的開始時間不是音效的問題。比如需要立即發揮的槍聲。 就代碼更改而言,MIDI和WAV只是不同的編碼類型,通用播放器可以播放任何類型,因此代碼更改基本上是nill。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM