微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jnius.jnius.JavaException:JVM 异常发生:setAudioSource 失败

如何解决jnius.jnius.JavaException:JVM 异常发生:setAudioSource 失败

由于 PyAudio 与 android 不兼容,尝试在 python 中使用以下代码jnius

    if platform == 'android':
        from android.permissions import request_permissions,Permission

        request_permissions([Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE,Permission.INTERNET,Permission.RECORD_AUdio,Permission.CAPTURE_AUdio_OUTPUT])

        # get the needed Java classes
        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')
        #String = autoclass("java.lang.String")

        # create out recorder
        self.mRecorder = self.MediaRecorder()
        self.mRecorder.setAudioSource(self.AudioSource.MIC)
        self.mRecorder.setoutputFormat(self.OutputFormat.MPEG_4)
        self.mRecorder.setoutputFile('/sdcard/test_recording.mp4')
        self.mRecorder.setAudioEncoder(self.AudioEncoder.AMR_NB)
        self.mRecorder.prepare()

但是当应用尝试加载时出现异常

jnius.jnius.JavaException: JVM exception occurred: setAudioSource Failed

然后应用程序崩溃了。 从代码中可以看出,我尝试授予所有权限。应用加载时,它会请求媒体权限,但不会请求麦克风权限。

我检查了麦克风的设置,令人惊讶的是我的应用程序甚至没有在那里列出以启用(可能是因为安装源不是 Playstore)。

我该如何克服这个问题?任何帮助表示赞赏。

此外,问题的根本原因是录制音频的权限被拒绝。这是在我请求 RECORD_AUdio 权限时发生的。

01-07 14:08:41.497 1417 1834 D 兼容性信息:applicationScale - 1.0 01-07 14:08:41.499 2331 2693 I ProcessMonitor: onForegroundInfoChanged: ForegroundInfo{mForegroundPackageName='com.google.android.packageinstaller',mForegroundUid=10023,mForegroundPid=2558,mLageNamenet.uicePackage=2558,mLage.,mLastForegroundPid=4691,mMultiWindowForegroundPackageName='null',mMultiWindowForegroundUid=-1,mFlags=0} 01-07 14:08:41.499 2331 2693 I GameBoosterService: onForegroundInfoChanged: Cur=com.google.android.packageinstaller last=jigyasu.net.voiceasstmob 01-07 14:08:41.499 794 18980 W ServiceManager:权限失败:android.permission.RECORD_AUdio from uid=10197 pid=4691 01-07 14:08:41.500 794 18980 E MediaRecorderService:请求需要 android.permission.RECORD_AUdio 01-07 14:08:41.500 2331 2693 D GameBoosterService: onGameStatusChange foreground:ForegroundInfo{mForegroundPackageName='com.google.android.packageinstaller',mForegroundPid=25558,mLageNameastjitjivoe mLast. 10197,mLastForegroundPid=4691,mMultiWindowForegroundPackageName='null',mMultiWindowForegroundUid=-1,mFlags=0} 01-07 14:08:41.500 2331 2693 I GameBoosterService:onForegroundInfoChanged:退出 01-07 14:08:41.500 2331 2693 I VideoBoxUtils: isSupportVideoBox: false 01-07 14:08:41.501 2331 2693 I VideoToolBoxService:onForegroundInfoChanged:退出 Vtb 01-07 14:08:41.503 4691 4714 I python:回溯(最后一次调用):

有趣的是,我没有在手机的麦克风权限下找到我的应用程序,这意味着我也无法手动授予权限。 该应用是否存在一些根本性问题?

解决方法

如果将来有人面临同样的问题,我找到了答案: 在您的规范文件中提及 RECORD_AUDIO 的权限。虽然可以通过代码询问存储权限,但必须在spec文件中提及MICROPHONE权限

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。