再生時間が数秒以上ある音声の場合、全てRAM上に読み込むのではなくディスク上の保存場所から直接ストリーミング再生するのが良いでしょう Libgdx ではこれを行うためのMusic インタフェースが用意されています。
Music インスタンスを読み込むには、以下のようにできます:
Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3"));
上記では内部のdata
ディレクトリから"mymusic.mp3"
という名前のMP3ファイルを読み込んでいます。
Music インスタンスの再生は以下のようにして行います:
music.play();
もちろん、Music
インスタンスの様々な再生設定を変更できます:
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
インスタンスは重いので、通常は2つ以上読み込まないようにしてください。
不要になった場合は、Music
インスタンスを破棄してリソースを解放する必要があります。
music.dispose();
Streaming musicFor any sound that's longer than a few seconds it is preferable to stream it from disk instead of fully loading it into RAM. Libgdx provides a Music interface that lets you do that. To load a Music instance we can do the following: Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3")); This loads an MP3 file called Playing back the music instance works as follows: music.play();
Of course you can set various playback attributes of the 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
A music.dispose();
|