• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps OpenAL for Android + Java interface

paulscode

Member
Mar 18, 2010
86
14
Hello all,

I'd like to announce my port of OpenAL to Android. This one includes a Java interface, which will allow you to add OpenAL to your Android projects without having to play around with c/c++ and JNI. It should work on all versions of Android back to 1.5.

For those familiar with using OpenAL under normal Java, the 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:
Code:
ALAN.create();
From there, you can access whatever AL functions you need through ALAN. For example:
Code:
        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:
Code:
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 ;D

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:
Code:
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. This demo is really low-tec.. I just threw everything into the activity's onCreate method, and the thread blocks there in the stream loop until playback finishes. It is just for demonstration purposes - you wouldn't normally write an app that way. You'll also notice that the make-shift ogg decoder I threw together is really inefficient (takes encoded buffers from Java, decode them in native, sends then back to Java, then Java sends them back to native to be played). I wouldn't recommend copying any part of that demo in a real-world app - it is entirely 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, they 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). This is the first release, so there are bound to be a few glitches to work out.
 
I haven't looked into OpenSL ES, I'll have to try it out to see how it compares. I don't notice any latency on my phone (Droid X), but haven't gotten a lot of feedback on other devices yet.

The compiled native library includes a version compatible on ARM5 and above devices, and a version for ARM7a and above. That covers just about every Android device out there. The code for the OpenAL and Java components are also be usable on x86 devices (e.g. Intel Atom) if they are recompiled for that target platform. The included Tremolo .ogg decoder library is the only component that would not be compilable on x86 devices, because it includes some ARM assembly sourcecode (Tremolo isn't part of OpenAL anyway, so not a big problem).

Obviously, this isn't the only OpenAL port to Android out there. My guess is that it is probably about the same performance-wise as other ones. The useful characteristic of this particular port is a Java interface similar to JOAL and LWJGL, making it useful for folks who like programming in Java and don't want to play around with c/ c++.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones