简体   繁体   中英

How to get audio (Mic) input working on Android with python/kivy

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM