3.7 C
New York
Saturday, February 22, 2025

ios – Can’t get microphone to work correctly in WebView


I am having hassle getting the microphone to work inside a WebView.

There is a webpage that I do know I can use to check my microphone. It really works completely in Safari. Once I discuss, I can see the bursts on the take a look at space that correspond to what I am saying.

I wrote an app to show the location in a WebView. I run the app, click on Permit when it asks if I wish to enable the location to make use of my microphone. Once I discuss, the bursts on the take a look at space do NOT correspond to what I am saying. In actual fact, the bursts in a kind of heartbeat sample. It is prefer it does not hear my voice in any respect. It looks as if a take a look at sample.

I really feel like I am doing one thing clearly flawed. Any assist can be appreciated.

I am operating the next:

  • Sonoma 14.6.1
  • Xcode 15.4
  • Simulator 15.4 (iPhone 15 professional)

Here is the code:

ContentView.swift

import UIKit
import WebKit
import AVFoundation

class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
    var webView: WKWebView!

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        // Setup WKWebView
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(body: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(webView)
        
        // Add script message handler
        let contentController = WKUserContentController()
        contentController.add(self, title: "logHandler")
        webConfiguration.userContentController = contentController

        // Add Auto Structure constraints
        NSLayoutConstraint.activate([
            webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            webView.topAnchor.constraint(equalTo: view.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        openMicrophoneTestPage()
    }
    

    non-public func openMicrophoneTestPage() {
        let testURL = URL(string: "https://mictests.com/")!
        let request = URLRequest(url: testURL)
        webView.load(request)
    }
    
    // WKScriptMessageHandler technique
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.title == "logHandler" {
            if let messageBody = message.physique as? String {
                print("JavaScript log: (messageBody)")
            }
        }
    }
}

WebViewControllerWrapper.swift

import SwiftUI
import UIKit

struct WebViewControllerWrapper: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> WebViewController {
        return WebViewController()
    }

    func updateUIViewController(_ uiViewController: WebViewController, context: Context) {
        // No replace logic wanted
    }
}

MicrophoneApp.swift

import SwiftUI

@essential
struct MicrophoneApp: App {
    var physique: some Scene {
        WindowGroup {
            WebViewControllerWrapper()
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles