對(duì)于任何長(zhǎng)于幾秒鐘的聲音,最好直接從磁盤讀取德谅,而不是將其完全加載到RAM內(nèi)存中, Libgdx提供了一個(gè)Music 接口,讓您可以做到這一點(diǎn)谋逻。
要加載一個(gè)音樂(lè)實(shí)例冠摄,我們可以執(zhí)行以下操作:
Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3"));
這會(huì)從文件目錄為data的內(nèi)部目錄中加載一個(gè)名為“mymusic.mp3”的MP3文件,播放音樂(lè)實(shí)例的代碼如下:
music.play();
當(dāng)然,您可以設(shè)置Music實(shí)例的各種播放屬性:
music.setVolume(0.5f); // sets the volume to half the maximum volume
music.setLooping(true); // will repeat playback until music.stop() is called
music.stop(); // stops the playback
music.pause(); // pauses the playback
music.play(); // resumes the playback
boolean isPlaying = music.isPlaying(); // obvious :)
boolean isLooping = music.isLooping(); // obvious as well :)
float position = music.getPosition(); // returns the playback position in seconds
Music相當(dāng)耗內(nèi)存拓哟,通常不超過(guò)兩個(gè)實(shí)例.如果不再需要使用Music實(shí)例請(qǐng)及時(shí)釋放資源想许。
music.dispose();