User Tools

Site Tools


Pocketsphinx on Android


Current tutorial describes a most recent verison pocketsphinx-android-demo-5prealpha which is available in downloads

PocketSphinx Android demo

Building and running from Eclipse

You should have ADT plugin installed. Also will need Android SDK installed either manually or with ADT plugin. Important You also need to have Eclipse Java development tools plugin installed, if you are using stock ADT from eclipse, go to Help→Install New Software. Select Eclipse Repository and install from Programming Languages section “Eclipse Java Development Tools”. Without that language model resources will not be updated properly.

  1. Download the source code version 5prealpha.
  2. Go to File → Import → Android → Existing Android Code Into Workspace.
     Import existing project
  3. Specify path to the source directory.
     Chose the source directory
  4. Go to Project → Properties → Android and set the project build target.
     Build target
  5. Go to Window → Show View → Other → LogCat to open logcat window.
     LogCat view
  6. Go to “Run → Run” to run the application. You must have physical device attached or virtual device setup. Make sure you attached the content of logcat if you are asking questions on the forum.
     LogCat content

Building and running from Android Studio

There is a separate package with the pocketsphinx demo for Android Studio, you can download it from downloads Pocketsphinx Android Demo for Android Studio. The code is identical to the Eclipse project, it is only arranged a bit differently according to a new project layout.

To try it import this project into IDE and run as usual, check logcat for details if something doesn't work.

Building and running from command-line

  1. Attach your physical device or setup a virtual device.
  2. Run
    ant debug install

    It will build and install application signed with a debug key. To build and install a release version run

    ant release install
  3. Manually run the application from the device application menu.

Using pocketsphinx-android

Referencing the library in Android project

Library is distributed as architecture-independent pocketsphinx-android-0.8-nolib.jar and binary .so files for different hardware architectures. Due to a bug in ADT plugin it is currently impossible to include pocketsphinx-android as a single jar.

You will have to use “-nolib” version of the library and add shared libraries manually. For example, if you are building application for ARM architecture on Eclipse, you will have to include “libs/pocketsphinx-android-0.8-nolib.jar” and “libs/armeabi/” into your project. In Android Studio you need to place jar file in app/libs folder and jni .so files into app/src/main/jniLibs folder.

Including resource files

The standard way to ship resource files with your application in Android is to put them in assets/ directory of your project. But in order to make them available for pocketsphinx files should have physical path, as long as they are within .apk they don't have one. Assets class from pocketsphinx-android provides a method to automatically copy asset files to external storage of the target device. edu.cmu.pocketsphinx.Assets#syncAssets synchronizes resources reading items from assets.lst file located on the top assets/. Before copying it matches MD5 checksums of an asset and a file on external storage with the same name if such exists. It only does actualy copying if there is incomplete information (no file on external storage, no any of two .md5 files) or there is hash mismatch. PocketSphinxAndroidDemo contains ant script that generates assets.lst as well as .md5 files, look for assets.xml. Please note that if ant build script doesn't run properly in your build process, assets might be out of sync. Make sure that script runs or create md5 files and assets.lst yourself.

If you are using Eclipse ADT it might be the case that you are missing ANT launching plugin. In that case you will get build errors on every build and a message like this:

!MESSAGE Launch configuration type id "org.eclipse.ant.AntBuilderLaunchConfigurationType" does not exist.
    2.    Possible causes:
    3.            Missing specification of a launch type (missing plug-in)
    4.            Incorrect launch configuration XML

To solve this issue install Java Development Tools in Eclipse (see instructions above).

Sample application

The classes and methods of pocketsphinx-android were designed to resemble the same workflow used in pocketsphinx, except that basic data structures organized into classes and functions working with them are turned into methods of the corresponding classes. So if you are familiar with pocketsphinx you should feel comfortable with pocketsphinx-android too.

SpeechRecognizer is the main class to access decoder functionality. It is created with the help of SpeechRecognizerSetup builder. SpeechRecognizerBuilder allows to configure main properties as well as other parameters of teh decoder. The parameters keys and values are the same as those are passed in command-line to pocketsphinx binaries. Read more about tweaking pocketsphinx performance.

        File modelsDir = new File(assetsDir, "models");
        recognizer = defaultSetup()
                .setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
                .setDictionary(new File(modelsDir, "dict/cmu07a.dic"))

Decoder configuration is lengthy process that contains IO operation, so it's recommended to run in inside async task.

Decoder supports multiple named searches which you can switch in runtime

        // Create keyword-activation search.
        recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
        // Create grammar-based searches.
        File menuGrammar = new File(modelsDir, "grammar/menu.gram");
        recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
        File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
        recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
        // Create language model search.
        File languageModel = new File(modelsDir, "lm/weather.dmp");
        recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);

Once you setup the decoder and add all the searches you can start recognition with


You will get notified on speech end event in onEndOfSpeech callback of the recognizer listener. Then you could call recognizer.stop or recognizer.cancel(). Latter will cancel the recognition, former will cause the final result be passed you in onResult callback.

During the recognition you will get partial results in onPartialResult callback.

You can also access other Pocketsphinx method wrapped with Java classes in swig, check for details Decoder, Hypothesis, Segment and NBest classes.

Building pocketsphinx-android

Pocketsphinx is provided with prebuilt binaries and it's not easy to compile it on various platforms. You shouldn't build it unless you understand what you are doing. Use prebuilt binaries instead.

Build dependencies

Building steps

  1. Create empty directory and set it as the current working directory.
  2. Checkout pocketsphinx-android sources.
  3. Checkout pocketsphinx sources.
  4. Checkout sphinxbase sources.
  5. Create new file “” and provide the following properties:
    • sdk.dir - path to Android SDK
    • ndk.dir - path to Android NDK
    • sdk.version - Android SDK version
  6. Go to “pocketsphinx-android” and run
    ant jar

Make sure you have “pocketsphinx-android” in the same parent directory as “pocketsphinx” and “sphinxbase” are as the building script refers them using exactly these names. If configured everything correctly you will find multiple shared libraries in “lib” - each in the directory with the target platform name, and a jar pocketsphinx-android-version-nolib.jar in “build” which you need to include into your application.

tutorialandroid.txt · Last modified: 2015/02/15 17:14 by admin