ios – WKWebView play YouTube video stops digital camera and microphone enter

Spread the love

I’m creating an iOS native app with Swift.

The app has a single UIView, which comprises the highest 50% with WKWebVeiw, which masses YouTube video URL (Additionally I attempted Google’s youtube_ios_player_helper YTPlayerView, which principally is a WKWebView as nicely)

Within the backside half of UIView I embedded AVCaptureVideoPreviewLayer, which reveals what’s in entrance of my iPhone digital camera.

Additionally on the similar time I initialised SFSpeechRecognizer to seize the consumer’s voice.

The AVCaptureVideoPreviewLayer and SFSpeechRecognizer are working tremendous earlier than I press the YouTube “Play” button in my WKWebView

Nevertheless, as soon as I press YouTube “Play” button in WKWebView (The YouTube video begin enjoying), AVCaptureVideoPreviewLayer freezes, and SFSpeechRecognizer stopes working.

On the following is how I create the WKWebView to load YouTube video:

        var youTubePlayerWebView: WKWebView! = WKWebView(body: CGRect(x: videoplayerX, y: videoplayerY, width: videoplayerWidth, peak: videoplayerHeight))
        youTubePlayerWebView.load(URLRequest(url: videoUrl))

On the following is how I create digital camera

        self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session!)
        self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        self.previewLayer?.connection!.videoOrientation = AVCaptureVideoOrientation.portrait
        self.previewLayer?.body = self.cameraPreview.bounds

On the following is how I monitor the consumer’s voice:

    guard let node = audioEngine?.inputNode else { return }
    let recordingFormat = node.outputFormat (forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
        self.request.append (buffer)
    audioEngine?.put together ()
    do {
        strive audioEngine?.begin()
        let audioSession = AVAudioSession.sharedInstance()
        strive audioSession.setCategory(AVAudioSession.Class.playAndRecord)
        strive audioSession.setMode(AVAudioSession.Mode.default)
        strive audioSession.setActive(true, choices: .notifyOthersOnDeactivation)
        strive AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
    } catch {
        return print (error)
    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { end result, error in
        if let end result = end result {
           //....voice recognition logic

I would like a strategy to enable my digital camera and microphone proceed working whereas WKWebView is enjoying YouTube video.

Leave a Reply

Your email address will not be published. Required fields are marked *