AVPlayer 在离开视图时暂停视频。

AVPlayer pause video when leaving view.

有没有人知道如何在退出视图时暂停 AVPlayer?我到处搜索,没有找到答案。

我尝试在 viewWillDissappear 和 viewDidDisappear 中添加 "player pause" 函数。似乎没有人明白。不知道它是否与滚动视图有关,但似乎应该有办法。也没有在 Apple 文档中看到任何内容。

这是我试过的。

//
 //  VideoTestViewController.swift
//  Faces of the Holocaust Photographic History
//
//  Created by New User on 8/26/17.
//  Copyright © 2017 New User. All rights reserved.
//

import UIKit
import AVFoundation
import AVKit



class VideoTest: UIViewController {


var player : AVPlayer?


override func viewDidLoad() {
    super.viewDidLoad()

    let url : URL = URL(string: "https://firebasestorage.googleapis.com/v0/b/faces-of-the-holocaust.appspot.com/o/Alan%20Hall.mp4?alt=media&token=19c13572-b6fb-42e9-94e5-535dac227de5")!
    let player = AVPlayer(url: url)
    var playerLayer : AVPlayerLayer?
    playerLayer = AVPlayerLayer(player: player)
    playerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
    playerLayer!.frame = self.view.frame
    self.view!.layer.addSublayer(playerLayer!)
    player.play()


    // Do any additional setup after loading the view.
}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}


override func viewWillDisappear(_ animated: Bool){

player?.pause()
}
*/

@IBAction func button(_ sender: Any) {
    let playerIsPlaying = player?.rate != 0
    if playerIsPlaying {
        player?.pause()
    } else {
        player?.play()
    }

}

}

原来你必须使用 viewDidDisappear 而不是 viewWillDisappear