在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController,您需要遵循以下步骤:
import MediaPlayer
var moviePlayer: MPMoviePlayerController?
func playVideo(url: URL) {
moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer?.prepareToPlay()
moviePlayer?.view.frame = view.bounds
moviePlayer?.scalingMode = .aspectFit
moviePlayer?.controlStyle = .fullscreen
moviePlayer?.shouldAutoplay = true
moviePlayer?.setFullscreen(true, animated: true)
view.addSubview(moviePlayer!.view)
}
playVideo(url: URL(string: "https://example.com/video.mp4")!)
NotificationCenter.default.addObserver(self, selector: #selector(videoPlaybackDidFinish), name: .MPMoviePlayerPlaybackDidFinish, object: nil)
@objc func videoPlaybackDidFinish(notification: Notification) {
if let reason = notification.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] as? Int {
if reason == MPMovieFinishReason.playbackEnded.rawValue {
moviePlayer?.setFullscreen(false, animated: true)
moviePlayer?.stop()
moviePlayer?.view.removeFromSuperview()
moviePlayer = nil
}
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: .MPMoviePlayerPlaybackDidFinish, object: nil)
}
这样,您就可以在iOS 3.2(iPad)中正确显示和关闭全屏MPMoviePlayerController了。请注意,MPMoviePlayerController已被弃用,建议使用AVPlayerViewController替代。
领取专属 10元无门槛券
手把手带您无忧上云