M1X
M1X

Reputation: 5354

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.

Upvotes: 5

Views: 1092

Answers (1)

Dmitriy Mitiai
Dmitriy Mitiai

Reputation: 1200

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.

Upvotes: 2

Related Questions