It’s taken me quite some time to get to this, but here it is: All new Sound methods! Let’s get to the core of it.
Every time you call Sound#play() you will get a long. This long identifies that instance of the Sound being played back at the moment. You can use this id to manipulate that sound instance while its playing: loop it, change it’s pitch, volume and panning!
And here is the Sound interface for the lazy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
public interface Sound extends Disposable { /** Plays the sound. If the sound is already playing, it will be played again, concurrently. * @return the id of the sound instance. */ public long play (); /** Plays the sound. If the sound is already playing, it will be played again, concurrently. * @param volume the volume in the range [0,1] * @return the id of the sound instance */ public long play (float volume); /** * Plays the sound, looping. If the sound is already playing, it will be played again, concurrently. * @return the id of the sound instance */ public long loop(); /** * Plays the sound, looping. If the sound is already playing, it will be played again, concurrently. * @param volume the volume in the range [0, 1] * @return the id of the sound instance */ public long loop(float volume); /** Stops playing all instances of this sound. */ public void stop (); /** Releases all the resources. */ public void dispose (); /** * Stops the sound instance with the given id as returned by {@link #play()} or {@link #play(float)}. If * the sound is no longer playing, this has no effect. * @param soundId the sound id */ public void stop(long soundId); /** * Sets the sound instance with the given id to be looping. If the sound is no longer playing this has no effect.s * @param soundId the sound id * @param looping whether to loop or not. */ public void setLooping(long soundId, boolean looping); /** * Changes the pitch multiplier of the sound instance with the given id as returned by {@link #play()} or {@link #play(float)}. If * the sound is no longer playing, this has no effect. * @param soundId the sound id * @param pitch the pitch multiplier, 1 == default, >1 == faster, <1 == slower */ public void setPitch(long soundId, float pitch); /** * Changes the volume of the sound instance with the given id as returned by {@link #play()} or {@link #play(float)}. If * the sound is no longer playing, this has no effect. * @param soundId the sound id * @param volume the volume in the range 0 (silent) to 1 (max volume). */ public void setVolume(long soundId, float volume); /** * Sets the panning and volume of the sound instance with the given id as returned by {@link #play()} or {@link #play(float)}. If the * sound is no longer playing, this has no effect. * @param soundId the sound id * @param pan panning in the range -1 (full right) to 1 (full left). 0 is center position. * @param volume the volume in the range [0,1]. */ public void setPan(long soundId, float pan, float volume); } |
Pretty simple eh? I’ll clean up the small inconsistencies in the Javadocs asap when we do our Javadoc sweep next week.
Three things:
- setPan() takes the volume as well. Why? Cause i can’t get a hold of the volume of a stream (==sound instance) via Android’s SoundPool and keeping a list is tedious and bound to create leaks and GC invocations.
- There is not Sound#isPlaying(long soundId). Why? Cause SoundPool doesn’t give me that information. There is no good work around for that (e.g. figuring out the sound length in seconds, and meassuring how long the sound’s been played back does not work as we don’t have an idea about latency on Android)
- If a sound id is no longer valid and you pass it to any of the methods above… nothing will happen ๐
- I might have mixed up linear and logarithmic gain/volume along the way for panning. If you find a problem let me know. The OpenAL implementation of panning is kinda hackish but is “good enough”(tm)
One more thing while i’m here: i haven’t been to the forums for a week, i’m sorry. Things are crazy at the moment and on top of that Stef came to SF. This means i won’t be back to the forums for another week.
Enjoy (and test)!
Sounds good ๐
I can’t wait to finish the visual of my
game and start adding music and effects…
Thanks !
I think that the only missing one is “isPlaying()” ๐
Waiting for that…
Read the blog post more closely. As in: actually read it ๐
You did even the pitching! Awesome, thanks.
That is a great addition!
Funny coincidence, I was playing with FMOD on my Android device recently ๐
Granted, FMOD is super-powerful in comparison with LibGDX (3D spatial audio, real-time DSP effects pipeline, etc.), but writing NDK code in C/C++ with a JNI interface to some Java host is a real pain in the arse.
Thanks for your hard work!
Cheers, Dan
References:
http://broadcast.oreilly.com/2011/06/fmod-for-android.html
http://www.fmod.org/index.php/download
Nice!
I still haven’t checked out all the new stuff in the latest update ๐
Thanks!
Hey guys, sorry if I’m not updated. but why don’t you add support for midi files? I never understood why you didn’t.
Thanks for all your hard work!
There is no pause and resume. So a long sound and you hit the pause button. So how do you want to resume the sound?
Wing, in the wiki it says: “Libgdx will automatically pause and resume all audio playback for you if your application is paused and resumed.” http://code.google.com/p/libgdx/wiki/Audio