Skip to content
Advertisement

How to run python voice recognition script in Java (Android Studio)?

I’ve searched quite a bit on this topic, looking at options like Jython and Chaquopy, but whatever I do, there is always an error.

What I’m looking to do is, when a button on my Android app is clicked, the python script (voice recognition using gTTS API in python) will run.

This is what my code looks like (using Chaquopy):

MainActivity.java:

JavaScript

Top-level build file: build.gradle

JavaScript

build.gradle (:app):

JavaScript

voice_recognition.py (located in python folder under the Main folder for Android Studio project):

JavaScript

I know Chaquopy works since I tested a basic python file and my emulator didn’t crash. Here is the error I get when clicking the button to run the python script:

JavaScript

Any suggestions of how to proceed? I really rather keep my Android app in Java since the interface is already implemented. How can I fix my code so it works with Chaquopy? Or how would I do it in Jython if it is a better alternative?

Advertisement

Answer

You’ll have to install gtts, into your app using pip, as described here. The same goes for the other third-party modules you’re using.

Also, you won’t be able to access files on your Windows desktop from an Android app. Instead, include them in your Python source directory and then access them relative to __file__ as described here.

Unfortunately this script will probably encounter other, more difficult problems:

  • You probably won’t be able to access the device’s microphone or speakers from Python code, so you’ll have to use the Android Java APIs instead.
  • As discussed here, recognize_google requires a FLAC converter, which Chaquopy isn’t currently able to support.

Sorry I couldn’t be more helpful.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement