android 中的 AudioRecorder 使用 python

AudioRecorder in android using python

我有一个问题:

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!

我在 Kivy 方面经验不多,但根据我开发一些原生 android 应用程序的经验,看起来你没有要求对你的代码进行录音许可。

Based on this video(从 27:48 开始),您需要请求访问权限,将您的代码放入如下内容:

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)

记得在使用麦克风之前请求录音许可。

您可以读取所有权限here