Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView play audio when app goes to background

I'm using YouTube iOS Helper Library to play a video inside a WKWebView and I want the audio to continue playing even when HOME is pressed or lock key.

Capabilities tab looks like this:

enter image description here

I have also imported import AVFoundation inside AppDelegate

and inside func applicationDidBecomeActive(_ application: UIApplication) I have this code:

func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch {
            // Handle setCategory failure
            print(error)
        }
    }

When app enters background audio stops and I can not even resume it from control center.

like image 590
M1X Avatar asked Oct 30 '22 03:10

M1X


1 Answers

For those who are searching for the similar solutions, want to tell, that play YouTube audio without displaying the video violates the terms of services of YouTube And your app will be rejected from App Store because of this.

like image 70
Dmitriy Miyai Avatar answered Nov 15 '22 05:11

Dmitriy Miyai