Skip to content

fix(android): grant WebView capture permissions for getUserMedia#145

Open
philippspeck wants to merge 1 commit into
NativePHP:mainfrom
philippspeck:fix/android-webview-getusermedia-audio
Open

fix(android): grant WebView capture permissions for getUserMedia#145
philippspeck wants to merge 1 commit into
NativePHP:mainfrom
philippspeck:fix/android-webview-getusermedia-audio

Conversation

@philippspeck
Copy link
Copy Markdown

Closes #144

Problem

navigator.mediaDevices.getUserMedia() fails on Android in NativePHP Mobile apps even when RECORD_AUDIO is granted in system settings.

Two gaps in the stock Android WebView setup:

  1. WebChromeClient.onPermissionRequest is missing — the WebView never grants RESOURCE_AUDIO_CAPTURE to web content → NotAllowedError: Permission denied.
  2. MODIFY_AUDIO_SETTINGS is not merged with RECORD_AUDIO — after (1), Chromium logs Unable to select communication device!NotReadableError: Could not start audio source.

Reproduced on Pixel 7 / Android 16 / WebView 148 with Livewire + Web Audio API voice recording.

Fix

WebViewManager.kt

Implement onPermissionRequest and grant capture resources when the matching Android runtime permission is already held:

  • RESOURCE_AUDIO_CAPTURE → requires RECORD_AUDIO
  • RESOURCE_VIDEO_CAPTURE → requires CAMERA

AndroidPluginCompiler.php

When merging plugin manifest permissions, also inject android.permission.MODIFY_AUDIO_SETTINGS whenever RECORD_AUDIO is present (in the plugin permission list or already in the manifest). Chromium needs this for WebView audio routing / communication device selection.

Test plan

  • getUserMedia({ audio: true }) succeeds on Pixel 7 (Android 16) after OS mic permission is granted
  • Logcat shows onPermissionRequest: android.webkit.resource.AUDIO_CAPTURE followed by grant
  • No Unable to select communication device! / NotReadableError when RECORD_AUDIO is in manifest
  • getUserMedia({ video: true }) still respects CAMERA runtime permission
  • Apps without RECORD_AUDIO in manifest do not receive MODIFY_AUDIO_SETTINGS unless they already declare microphone capture

Made with Cursor

Implement WebChromeClient.onPermissionRequest so RESOURCE_AUDIO_CAPTURE
(and VIDEO_CAPTURE when CAMERA is granted) are passed through to web
content. Without this handler, getUserMedia() always fails with
NotAllowedError even when RECORD_AUDIO is granted at the OS level.

Also inject MODIFY_AUDIO_SETTINGS whenever RECORD_AUDIO is present in
the merged manifest. Chromium logs "Unable to select communication
device!" and getUserMedia() fails with NotReadableError without it.

Closes NativePHP#144

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android WebView: getUserMedia fails (missing onPermissionRequest + MODIFY_AUDIO_SETTINGS)

1 participant