我正在使用 AVPlayerViewController 内联播放本地视频。
我想要的行为是在 PIP 窗口中播放的视频将在视频结束时自动关闭。
有没有办法用 AVPlayerViewController 做到这一点?
编辑:也发布在 Apple 开发者论坛 https://developer.apple.com/forums/thread/652748
import UIKit
import AVKit
class ViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var videoPath = "video.mov"
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
} catch { }
let player = AVPlayer(url: URL(fileURLWithPath: videoPath))
playerViewController.player = player
NotificationCenter.default.addObserver(
self,
selector: #selector(playerDidFinishPlaying),
name: .AVPlayerItemDidPlayToEndTime,
object: player.currentItem
)
playerViewController.showsPlaybackControls = true
playerViewController.entersFullScreenWhenPlaybackBegins = true
self.addChild(playerViewController)
self.view.addSubview(playerViewController.view)
}
@objc func playerDidFinishPlaying(note: NSNotification) {
// ???
}
}