VeinGuo / VGPlayer

📺 A simple iOS video player by Vein.
https://github.com/VeinGuo/VGPlayer
MIT License
400 stars 101 forks source link

UIAlertController is not Getting Dismissed in some Cases #71

Closed Rickrk closed 5 years ago

Rickrk commented 5 years ago

In my project a video will be processed and then after some effects it will be played. While processing the video I want to show Please wait... message.

I show an alert using UIAlertController and after finishing the process I want to hide the alert.

If the video processed correctly, the alert will be dismissed, but in some cases that the process failed dismiss also will not work! Note that dismiss function will be called, but it does not work.

class VGViewController: UIViewController {
    var player : VGPlayer?
    var url: URL?
    var outputVideo: URL?
    override func viewDidLoad() {
        super.viewDidLoad()
        showLoading()
        DispatchQueue.global(qos: .userInitiated).async {
            do {
                guard let output = try effectVideo(fileURL: url) else {
                    DispatchQueue.main.async {
                        self.hideLoading()
                    }
                    return
                }
                outputVideo = output
            } catch let e {
                DispatchQueue.main.async {
                        self.hideLoading()
                    }
                print("video error: ",e)
            }
            self.player = VGPlayer(URL: outputVideo)
            self.player?.delegate = self
            DispatchQueue.main.async {
                self.hideLoading()
                self.view.addSubview((self.player?.displayView)!)
                self.player?.backgroundMode = .proceed
                self.player?.play()
                self.player?.displayView.delegate = self
                self.player?.displayView.titleLabel.text = self.url!.lastPathComponent
                self.player?.displayView.snp.makeConstraints { [weak self] (make) in
                    guard let strongSelf = self else { return }
                    make.edges.equalTo(strongSelf.view)
                }
            }
        }
    }
    var alert: UIAlertController!
    private func showLoading() {
        alert = UIAlertController(title: nil, message: "Please wait.\nThis may take a few seconds...", preferredStyle: .alert)
        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating()
        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)
    }

    private func hideLoading() {
        alert.dismiss(animated: true, completion: {print("**** alert dismiss called ****")})
    }
}