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

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página