2

EDIT from a visitor: The current consensus around this situation appears to be that Kivy does not support the microphone at this time, and we are begging people to help port the "audiostream" add-on forward, so that this can work again. Any tiny work to help this is greatly appreciated. More information below.

I try to get mic working on android, im using mainly kivy and buildozer I got working audio out with audiostream, however that module is so outdated it wont work anymore if use input "recording" GITHub Issue .well i was unable to get recording working on pc because it says "unsupported" as soon i use record functions, on documents it mentions only mobile devices, so that is ok. it can be replaced on those platforms anyways with pyaudio.

I have tried to search other options what i can use, so i came up across pyjnius and MediaRecorder, im very novice with java,(and trying to learn python atm, so novice there too) so i was unable to get it working. The problem lies, i need to get all mic data into bytes, this is easy with pyaudio, and it works. reason why im here, it is android where pyaudio not work(at least i havent yet tried compile libraries to android, i know this might be possible but alot work..)

Here is code what i have to try get it working:

            MediaRecorder = autoclass('android.media.MediaRecorder')
            AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
            OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
            AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')
            FileOutputStream = autoclass('java.io.FileOutputStream')
            gaindata = io.BytesIO()
    
            mRecorder = MediaRecorder()
            mRecorder.setAudioSource(AudioSource.MIC)
            mRecorder.setOutputFormat(OutputFormat.THREE_GPP)

            mRecorder.setOutputFile(gaindata.getBytes())
            mRecorder.setAudioEncoder(AudioEncoder.AMR_NB)
            mRecorder.prepare()

I know there is something about FileDescriptor, there is some examples, but all them have spaces on strings so i have no idea how to convert them to python.. all i want is setOutputFile -> gaindata

If there is another option would be nice, i need bytesIO data from microphone(prefer 8000,mono,raw wav without header OR GSM6.10) and i will convert it with soundfile(yes i did compiled libsndfile.so to arm) into gsm6.10 and put it into socket, its a VoIP app.

fuzzyTew
  • 3,511
  • 29
  • 24
Riku
  • 21
  • 1
  • 3

2 Answers2

0

audiostream

audiostream uses pyjnius as well

https://github.com/kivy/audiostream/blob/master/audiostream/platform/plat_android.pyx

from jnius import autoclass
    AudioIn = autoclass('org.audiostream.AudioIn') 

I think best way would be fixing audiostream so other can use it as well because in the documentation of kivy it is mentioned to use it:

https://kivy.org/doc/master/api-kivy.core.audio.html

Note
The core audio library does not support recording audio. If you require this functionality, please refer to the audiostream extension.

Or you extract the core functionality of the project so you can use it.

pyaudio

Other project I found using microphone is https://pypi.org/project/SpeechRecognition/ which uses pyaudio

But I don't know if this works on android. Without your comment I would have thought it works because someone has created a kivy app to use it ...

https://github.com/jmercouris/speech_recognition

Community
  • 1
  • 1
Thomas Strub
  • 1,275
  • 7
  • 20
  • Thanks for answer. audiostream, Well it errors out ImportError: dlopen failed: cannot locate symbol "SDL_ANDROID_GetJNIEnv" dont know what to start fixing there.. pyaudio: well needs portaudio, and portaudio has no recipe. but i might able compile arm lib binaries to the android like i did on sndfile.so. looking peech_recognition is nothing mentioned about android.. only kivy. – Riku Jan 21 '20 at 10:51
  • For fixing the `SDL_ANDROID_GetJNIEnv` bug I would try to migrate audiostream to SDL2 http://wiki.libsdl.org/MigrationGuide#Audio and if it works bingo – Thomas Strub Jan 21 '20 at 11:28
  • okey after looking more deeply, it will use SDL2 with recipe, also was able to compile it with only sdl2. so that is not issue. but i took a look into android_ext.h there is `void audiostream_jni_register() { if ( !audiostream_jni_registered ) { JNIEnv *env = SDL_ANDROID_GetJNIEnv(); jclass cls = (*env)->FindClass(env, "org/audiostream/AudioIn"); (*env)->RegisterNatives(env, cls, methods, sizeof(methods) / sizeof(methods[0])); audiostream_jni_registered = 1; } } ` org/audi.. tried add android, there – Riku Jan 21 '20 at 17:03
  • it migh be seems it cannot find the file AudioIn or something.. hmm – Riku Jan 21 '20 at 17:05
  • Modify your question so it's easier to help – Thomas Strub Jan 21 '20 at 18:43
0

I have looked into it, you can still make recording audio works in PC using SpeechRecognition which uses PyAudio. For mobile, I don't think Buildozer support that because I tried it and it didn't work. audiostream seems to be outdated, but I haven't tried it yet. edit: i just tried using audiostream, but i can't seem to make it work.

Newbie101
  • 1
  • 1