Coming soon - Get a detailed view of why an account is flagged as spam!
view details
58
When playing a video in my app it pauses any background audio (music app, podcast, etc.) but I cannot get it to resume the user's audio when closing the video
Post Body

I have an app where users can play videos fullscreen. This is accomplished quite simply with AVPlayerViewController.

It works great, but in circumstances where the user is, say, listening to a podcast, if they tap on a video to watch it'll pause the podcast so that the user can watch the video, but when I close the video I would expect the podcast to resume. This is the behavior in most apps, I've confirmed it with Twitter and Tweetbot, for instance.

I've had absolutely no luck getting this to work myself however.

Things I've tried:

  • Just presenting the AVPlayerViewController with the minimum amount of code required: doesn't resume.
  • On viewDidDisappear of the AVPlayerViewController calling AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation): doesn't resume and spits out a "player is busy" error.
  • Okay, let's try stopping the player manually with player?.pause() in viewDidDisappear: doesn't resume, still gives a "player is busy" error.
  • Hmm, maybe give it a second? Try calling setActive after a DispatchQueue.main.asyncAfter of 1 second? No error, but doesn't resume.

I've put it in a test project with a very minimal amount of code:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(type: .system)
        button.addTarget(self, action: #selector(buttoned(sender:)), for: .touchUpInside)
        button.setTitle("Tap here to view video", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 19.0)
        button.frame = view.bounds
        view.addSubview(button)
    }

    @objc private func buttoned(sender: UIButton) {
        let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        let player = AVPlayer(url: videoURL!)
        let playerViewController = CustomVideoController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player?.play()
        }
    }
}

class CustomVideoController: AVPlayerViewController {
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        do {
            try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
        } catch {
            print(error)
        }
    }
}

Here's the complete compile-able project (literally just the above code): https://drive.google.com/file/d/1oWb-bNIQv5B1KjMkez6BxuEIh_n6eFxH/view

Play something from like Apple Music, Spotify, Overcast, etc. then run the app and tap view video.

I'm kinda going crazy trying to figure this relatively trivial thing out, as I'm sure I'm just doing something incredibly stupid, but any help would be appreciated.

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,715,057
Link Karma
529,042
Comment Karma
607,499
Profile updated: 5 days ago
Posts updated: 3 months ago
Objective-C / Swift

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago