5

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.

M1X
  • 4,971
  • 10
  • 61
  • 123
  • Pretty sure this is just a YouTube thing. What happens if you try a different website like Vimeo? – Randy the Dev Aug 26 '17 at 22:05
  • I'm using `youtube-player-ios-helper` not a website – M1X Aug 26 '17 at 22:06
  • 1
    I am trying to resolve the same problem too, with my own page in WKWebView using WebAudio APIs to produce sound. Every time the app goes into the background and later re-enters to the foreground, the WebAudio calls will all fail. I've tried calling AVAudioSession's setActive in applicationWillEnterForeground or applicationDidBecomeActive. Nothing worked so far. – Nibikibaba Jan 14 '18 at 03:14

1 Answers1

2

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.

Dmitriy Mitiai
  • 1,112
  • 12
  • 15