Interface AudioSource
- All Superinterfaces:
com.badlogic.gdx.utils.Disposable
Sometimes we just want the data for a sound or music asset without having to
explicitly play it. This is especially true when we are appending tracks to
a MusicQueue
. This class allows us to decouple this relationship.
This class also provides the user with a lot of "header" information that is not available in the basic LibGDX asset classes. This includes information like the number of channels or the sample rate. This gives users more programmatic control over their game audio.
All sample instances are created via AudioEngine.newSource(com.badlogic.gdx.files.FileHandle)
. When you
are done with using the sample instance you have to dispose it via the
Disposable.dispose()
method.
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of audio channels in this source.getData()
Returns a byte buffer encapsulating the audio assetfloat
Returns the duration of this audio asset in secondscom.badlogic.gdx.files.FileHandle
getFile()
Returns the file that generated this source.int
Returns the sample rate of this audio assetReturns a newAudioStream
to stream this asset.Methods inherited from interface com.badlogic.gdx.utils.Disposable
dispose
-
Method Details
-
getFile
com.badlogic.gdx.files.FileHandle getFile()Returns the file that generated this source.The currently supported formats are WAV, MP3 and OGG.
- Returns:
- the file that generated this source.
-
getChannels
int getChannels()Returns the number of audio channels in this source.A mono sound will have 1 channel, while a stereo sound will have two channels. While OGG and WAV support more than 2 channels, MP3 does not. Furthermore, LibGDX cannot playback any source with more than 2 channels in it.
- Returns:
- the number of audio channels in this source.
-
getSampleRate
int getSampleRate()Returns the sample rate of this audio assetThe sample rate is the number of samples per second of audio. CD quality sound has a sample rate of 44100. Many games use a sample rate of 48000.
- Returns:
- the sample rate of this audio asset
-
getDuration
float getDuration()Returns the duration of this audio asset in secondsThis value may be a slight approximation, given the variability within the different formats (WAV, MP3, OGG).
- Returns:
- the duration of this audio asset in seconds
-
getStream
AudioStream getStream()Returns a newAudioStream
to stream this asset.This is the interface used by
MusicQueue
to stream music from the file. A sample may have multiple independent streams. The will all be distinct from one another.- Returns:
- a new
AudioStream
to stream this asset.
-
getData
ByteBuffer getData()Returns a byte buffer encapsulating the audio assetThe byte buffer will be the complete audio asset, fully loaded into memory. While there is no upper limit on the audio file size for this method, you should avoid using this method for any audio asset greater than 1 MB. The byte representation of an audio source is platform dependent. You should avoid using this buffer directly unless you know what you are doing. If you need to read audio samples, get an
AudioStream
instead.- Returns:
- a byte buffer encapsulating the audio asset
-