【问题标题】:AudioRecorder in android using pythonandroid中的AudioRecorder使用python
【发布时间】:2021-12-21 14:24:56
【问题描述】:

我有一个问题:

这是我点击kivy按钮后的运行功能

而且我知道 time.sleep 在这里使用不当,但我怎么可能 换吗?

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.."

这是记录器类(一切正常吗?)

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')

这是我得到的错误:

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

谁能帮帮我?谢谢!

【问题讨论】:

    标签: python android kivy pyjnius


    【解决方案1】:

    我在 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的所有权限。

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      相关资源
      最近更新 更多