I am still working out the kinks with the Android version of my 3D Sound System. In the mean time, I thought some folks might find this part of the library useful by itself. This is my port of native OpenAL to Android with a simple Java interface. It will basically allow you to add OpenAL to your projects without having to play around with c/c++ and JNI. If optimization is a big concern for your project but you still need 3D sound, removing the overhead of SoundSystem and accessing OpenAL directly could be a good option. This library should work on all Android versions back to 1.5 (although I've only actually tested it on my own phone).
The Java interface for this port is probably closer to JOAL rather than LWJGL's interface. There are a few differences from JOAL, however (mainly in the methods that take array/ buffer parameters, the way the context is created and destroyed, and the lack of a direct interface to ALC with device and context objects Java-side). That being said, code written for JOAL or LWJGL is pretty simple to make work with my interface (it's all OpenAL when you get right down to the nuts and bolts, after all).
Everything is accessed statically through the class paulscode.android.sound.ALAN. Before creating any sources and what-not, first connect with the Android audio device and create an AL context by calling:
ALAN.create();
From there, you can access whatever AL functions you need through ALAN. For example:
int[] ALBufferIDs = new int[BUFFER_COUNT];
int[] source = new int[1];
int[] state = new int[1];
ALAN.alGenSources( 1, source );
ALAN.alGenBuffers( BUFFER_COUNT, ALBufferIDs );
ALAN.alBufferData( ALBufferIDs[i], format, buffer, buffer.length, rate );
ALAN.alSourceQueueBuffers( source[0], c, ALBufferIDs );
ALAN.alSourceQueueBuffers( source[0], c, ALBufferIDs );
ALAN.alSourcePlay( source[0] );
ALAN.alGetSourcei( source[0], ALAN.AL_SOURCE_STATE, state );
if( state[0] != ALAN.AL_PLAYING )
whatever();
And when you're finished, destroy the AL context and disconnect from the Android audio device by calling:
ALAN.destroy();
In case you haven't caught on, ALAN isn't a personal name like some AI alias - it's just an abbreviated form of "OpenAL for Android" (fewer keystrokes than OpenALAndroid). On a humorous note, I was actually forced to rename a hundred or so references to my project's original title "Android OpenAL", which I was horrified to realize became "ANAL" when abbreviated. THAT was a real pain in the butt
Anyway, here is a very basic app to demonstrate streaming an .ogg file:
ALAN Demo (
source code)
I normally drop in the source code to my projects, but you should be able to just unzip the above APK file, grab the contents of the "lib" folder, and paste them into your own project's "lib" folder. If you prefer to compile the source code yourself, you can unzip the source code archive above and either run the following commands from the terminal/command prompt:
ndk-build
ant debug
ant install
Or you can "create a new project from source" in Eclipse (requires the Android SDK and NDK to be installed, as well as the ADT plug-in and Sequoyah Android Native Code plugin from the update site plug-ins). Once you've created the Eclipse project, just right-click on it in the left panel and navigate to "Android Tools->Add Native Support".
The above demo utilizes the Tremolo library for decoding the .ogg file. It is a popular library for use on Android because it is extra light-weight and optimized for the ARM architecture, but there are plenty of other decoder libraries out there you could use if you have problems with that one. In theory, you could even use any of a number of pure-Java audio decoders as well, including the ones included with my SoundSystem library. I do not recommend copying the code from this demo - it very inefficient and meant only as a proof of concept.
One final note: OpenAL Android is licensed by the LGPL (since that is what OpenAL and Tremolo are licensed under). While I normally shy away from this license, it actually meshes really well with Android apps, because the end user can use any of a number of free backup programs (such as ASTRO) to create a backup APK of your app. From there, he can plug in a different version of the library into the APK, and simply reinstall it. All you have to do is mention somewhere in your documentation where they can acquire the sourcecode for OpenAL Android (feel free to mention
my website if you don't want to host the files yourself). Please let me know if you encounter any bugs (logcat output is always helpful).