我目前正在尝试在我的Swift应用中实现MPMoviePlayerController
。加载视频时(通过单击上一个视图控制器上的按钮),我希望视频进入全屏模式。使用我当前的代码,这在肖像中完美地工作,但是一旦视频旋转到横向,它只占用屏幕的一半。有什么方法可以解决这个问题吗?
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded}
以下是一些帮助解释我的意思的截图:
答案 0 :(得分:4)
视图没有调整大小,因为您的代码永远不会告诉它。
如果要在旋转时调整大小,则需要在容器视图因旋转而发生更改时调整MPMoviePlayerController
视图的大小。 (在viewWillLayoutSubviews
中设置帧或使用autolayout将MPMoviePlayerController
的视图约束到呈现控制器的视图。)
您也可以使用MPMoviePlayerViewController
代替MPMoviePlayerController
。来自文档:
与使用
MPMoviePlayerController
对象本身立即呈现电影不同,您可以将电影播放器视图控制器合并到通常使用视图控制器的任何位置。
你可以像这样使用它:
class MainViewController : UIViewController {
var movieViewController : MPMoviePlayerViewController?
override func viewDidLoad() {
var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
movieViewController = MPMoviePlayerViewController(contentURL: url)
movieViewController?.moviePlayer.fullscreen = true
movieViewController?.moviePlayer.controlStyle = .Embedded
}
override func viewDidAppear(animated: Bool) {
self.presentMoviePlayerViewControllerAnimated(movieViewController)
}
}
答案 1 :(得分:0)
对于Xcode 6.4,Swift 1.2:
<强> SOLUTION:强>
我在
中设置了全屏模式viewWillLayoutSubviews()
override func viewWillLayoutSubviews()
{
self.moviePlayer.fullscreen = true
}
完整代码:
import UIKit
导入MediaPlayer
class XXXMovieC: UIViewController
{
private var moviePlayer: MPMoviePlayerController!
var movieURL: NSURL!
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override func viewWillLayoutSubviews()
{
self.moviePlayer.fullscreen = true
}
override func viewDidLoad()
{
super.viewDidLoad()
setupMoviePlayer()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "movieFinishedCallback:", name: MPMoviePlayerWillExitFullscreenNotification, object: self.moviePlayer)
self.moviePlayer.shouldAutoplay = true
self.moviePlayer.prepareToPlay()
self.moviePlayer.play()
}
func setupMoviePlayer()
{
self.moviePlayer = MPMoviePlayerController(contentURL: self.movieURL)
self.moviePlayer.view.frame = self.view.frame
self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
self.view.addSubview(self.moviePlayer.view)
}
func movieFinishedCallback(notification:NSNotification)
{
self.dismissViewControllerAnimated(false, completion: { () -> Void in
self.movieURL = nil
})
}
}