呈现和关闭AVPlayer控制器的问题

时间:2019-01-15 19:43:25

标签: ios swift avfoundation

因此,我有一个应用程序可以拍摄和录制要发送到服务器并稍后观看的视频。

事件的顺序如下 1.录像 2.添加了过滤器或文字 3.发送到服务器 4.关闭avplayer并返回相机

一切正常,但在第四步,avplayer从未完全消失,我所看到的就是这个。

enter image description here

这是avplayer控制器上所有UI组件减去实际视频后的图像。

我确保我调用了present和dismiss,所以在使用错误的视图控制器表示和解雇方法组合时不会出现问题。当我进入此屏幕时,我必须再次按X才能回到相机,而不是这种情况。

/// Add the story to firebase
func handleAddToStory() {
    print("Attempting to add to story")
    self.dismiss(animated: true, completion: nil)
    // hide the color slider so we can return the image of the tapView that contains the text field if they added one
    colorSlider.isHidden = true
    colorSlider.isHidden = false





    DispatchQueue.global(qos: .background).async {
        print("This is run on the background queue")

        let videoImage = self.imageWithView(inView: self.tapView)


        // Export the video
        self.video?.exportFilterVideo(videoComposition: self.avVideoComposition , completion: { (url) in

            if let videoImage = videoImage {

                let filterVideoAsset = AVAsset(url: url! as URL)

                // Now merge the filtered video with tapView image which will contain the textfield if the user added one
                Merge(config: .standard).overlayVideo(video: filterVideoAsset, overlayImage: videoImage, completion: { (finalVideoUrl) in

                    // Upload to firebase storage
                    let dateFormatter = ISO8601DateFormatter()
                    let timeStamp = dateFormatter.string(from: Date())
                    let uid = User.current.uid
                    let storageRef = Storage.storage().reference().child("event_stories").child(self.eventKey).child(uid).child(timeStamp + ".mp4")
                    StorageService.uploadVideo(finalVideoUrl! as URL, at: storageRef) { (downloadUrl) in
                        guard let downloadUrl = downloadUrl else {
                            return
                        }

                        let videoUrlString = downloadUrl.absoluteString
                        print(videoUrlString)
                        // Post to firebase
                        PostService.create(for: self.eventKey, for: videoUrlString)
                    }

                }, progressHandler: { _ in })


            } else {

                let dateFormatter = ISO8601DateFormatter()
                let timeStamp = dateFormatter.string(from: Date())
                let uid = User.current.uid
                let storageRef = Storage.storage().reference().child("event_stories").child(self.eventKey).child(uid).child(timeStamp + ".mp4")
                StorageService.uploadVideo(url! as URL, at: storageRef) { (downloadUrl) in
                    guard let downloadUrl = downloadUrl else {
                        return
                    }

                    let videoUrlString = downloadUrl.absoluteString
                    print(videoUrlString)
                    PostService.create(for: self.eventKey, for: videoUrlString)
                }
                //svprogresshud insert here

            }

        })







        DispatchQueue.main.async {
            print("This is run on the main queue, after the previous code in outer block")
            self.dismiss(animated: true, completion: nil)
            self.videoPlayer?.replaceCurrentItem(with: nil)

        }
    }


}

这是处理添加到服务器的功能。正在进行一些异步任务,但是这些任务不应该篡改控制器的解雇,这可以从底部看到。有谁知道为什么视图控制器仍然存在?

下面是呈现上述视图控制器的代码段。

  if let event = self.event {
                        let video = AVURLAsset(url: videoURL)
                        let videoViewController = FilterVideoViewController(video: video)
                        videoViewController.event = event
                        SVProgressHUD.dismiss(completion: {
                            self.present(videoViewController, animated: false, completion: nil)
                        })

                    }

1 个答案:

答案 0 :(得分:1)

这些部分

video?.exportFilterVideo(videoComposition: avVideoComposition , completion: { (url) in 
   Merge(config: .standard).overlayVideo(video: filterVideoAsset, overlayImage: videoImage, completion: { (finalVideoUrl) in

使主线程非常繁忙。所以您需要像

这样的后台队列
 func handleAddToStory() {
    print("Attempting to add to story")

    // hide the color slider so we can return the image of the tapView that contains the text field if they added one
    colorSlider.isHidden = true
    let videoImage = self.imageWithView(inView: self.tapView)
    colorSlider.isHidden = false
    self.dismiss(animated:false, completion: nil)
    self.videoPlayer?.replaceCurrentItem(with: nil)


    DispatchQueue.main.asyncAfter(deadline: .now() + 0.7 ) {

            video?.exportFilterVideo(videoComposition: avVideoComposition , completion: { (url) in

                if let videoImage = videoImage {

                    let filterVideoAsset = AVAsset(url: url! as URL)

                    // Now merge the filtered video with tapView image which will contain the textfield if the user added one
                    Merge(config: .standard).overlayVideo(video: filterVideoAsset, overlayImage: videoImage, completion: { (finalVideoUrl) in

                        upload(finalVideoUrl! as URL)  

                    }, progressHandler: { _ in })


                } else {

                   upload(url! as URL)

                }

            })

    }


}

func upload(_ url:URL) {

 DispatchQueue.global().async {

    let dateFormatter = ISO8601DateFormatter()
    let timeStamp = dateFormatter.string(from: Date())
    let uid = User.current.uid
    let storageRef = Storage.storage().reference().child("event_stories").child(self.eventKey).child(uid).child(timeStamp + ".mp4")
    StorageService.uploadVideo(url, at: storageRef) { (downloadUrl) in
        guard let downloadUrl = downloadUrl else {
            return
        }

        let videoUrlString = downloadUrl.absoluteString
        print(videoUrlString)
        PostService.create(for: self.eventKey, for: videoUrlString)
    }

 }

}