pythonandroidkivypyjnius

AudioRecorder in android using python


i have a problem:

This is the run function after I click on the kivy button

And I know that time.sleep is used badly here, but how can I possibly replace it?

from jnius import autoclass

    def run(self):
        self.r = MyRecorder()
        self.r.mRecorder.prepare()
        self.r.mRecorder.start()
        self.console.text += "Recording.."
        time.sleep(1)
        self.r.mRecorder.stop()
        self.r.mRecorder.release()
        self.console.text += "Recording stopped.."

This is the recorder class (Is everything right?)

class MyRecorder:
def __init__(self):
    '''Recorder object To access Android Hardware'''
    self.MediaRecorder = autoclass('android.media.MediaRecorder')
    self.AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
    self.OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
    self.AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')

    # create out recorder
    self.mRecorder = self.MediaRecorder()
    self.mRecorder.setAudioSource(self.AudioSource.MIC)
    self.mRecorder.setOutputFormat(self.OutputFormat.THREE_GPP)
    self.mRecorder.setAudioEncoder(self.AudioEncoder.AMR_NB)
    self.mRecorder.setOutputFile('./MYAUDIO.3gp')

Here's the error I got:

if child.dispatch('on_touch_down', touch):    File "kivy/_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch    File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/kivy/uix/behaviors/button.py", line 151, in on_touch_down
     self.dispatch('on_press')    File "kivy/_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch    File "kivy/_event.pyx", line 1248, in kivy._event.EventObservers.dispatch    File "kivy/_event.pyx", line 1132, in kivy._event.EventObservers._dispatch  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/kivy/lang/builder.py", line 57, in custom_callback
     exec(kvlang.co_value, idmap)    File "/storage/emulated/0/python/my.kv", line 6, in <module>
     on_press: root.run()    File "/storage/emulated/0/python/main.py", line 51, in run
     startRecording(self)    File "/storage/emulated/0/python/main.py", line 139, in init
     self.mRecorder.setOutputFile('./MYAUDIO.3gp')    File "jnius/jnius_export_class.pxi", line 857, in jnius.jnius.JavaMethod.call    File "jnius/jnius_export_class.pxi", line 954, in jnius.jnius.JavaMethod.call_method    File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception  jnius.jnius.JavaException: JVM exception occurred: setAudioSource failed. java.lang.RuntimeException

Can anyone help me please? Thanks!


Solution

  • I don't have a lot of experience in Kivy, but based on my experience developing some native android Apps, looks like you didn't ask for record audio permission on your code.

    Based on this video (start on 27:48), you need to request access putting on your code something like:

    from android.permissions import Permission, request_permissions
    def callback_func(permission, results):
      if all([res for res in results):
        print("All permissions granted")
      else:
        print("Did not get all permissions")
    
    request_permissions([Permission.RECORD_AUDIO], callback_func)
    

    Remember to ask for the record permission BEFORE using the microphone.

    You can read all the permissions here.