要发布MPMoviePlayerController,您需要遵循以下步骤:
import MediaPlayer
import AVFoundation
let moviePlayer = MPMoviePlayerController(contentURL: URL(string: "https://example.com/path/to/video.mp4"))
moviePlayer.controlStyle = .fullscreen
moviePlayer.shouldAutoplay = true
moviePlayer.setVolume(1.0, animated: false)
view.addSubview(moviePlayer.view)
moviePlayer.view.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
moviePlayer.prepareToPlay()
NotificationCenter.default.addObserver(self, selector: #selector(moviePlaybackDidFinish(notification:)), name: .MPMoviePlayerPlaybackDidFinish, object: moviePlayer)
@objc func moviePlaybackDidFinish(notification: Notification) {
// 在此处理视频播放结束事件
}
deinit {
NotificationCenter.default.removeObserver(self, name: .MPMoviePlayerPlaybackDidFinish, object: moviePlayer)
}
通过遵循以上步骤,您可以成功发布MPMoviePlayerController并播放视频。
领取专属 10元无门槛券
手把手带您无忧上云