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 theAVPlayerViewController
callingAVAudioSession.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()
inviewDidDisappear
: doesn't resume, still gives a "player is busy" error. - Hmm, maybe give it a second? Try calling
setActive
after aDispatchQueue.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.
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/iOSProgramm...