MovieViewController 的奇怪方向行为

Strange orientation behaviour of MovieViewController

所以在我的应用程序中有一个 MPMoviePlayerViewController 允许用户观看视频。他们可以在 MPMoviePlayerViewController 中以任何方向观看视频,一旦他们完成并点击 "done" 他们 return 到前一个视图控制器,但仅限纵向。问题是当按下 'Done' 按钮时,View Controller 在 return 像它应该的那样切换到纵向之前会短暂地横向闪烁。这是 运行 时的样子:http://1drv.ms/1RSqUUD

附上我的代码:

import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var movieViewController : MPMoviePlayerViewController?
var movieplayer : MPMoviePlayerController!

override func viewDidLoad() {
    var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    movieViewController = MPMoviePlayerViewController(contentURL: url)
}



override func viewWillAppear(animated: Bool) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

@IBAction func WatchPressed(sender: AnyObject)
{
    self.presentMoviePlayerViewControllerAnimated(movieViewController)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}


func Rotated()
{
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
            movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
            movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

编辑

使用此代码时:(如下所示) http://txt.do/nt1s 当用户旋转到横向时,用户会被抛出电影播放器​​,并且在旋转到纵向之前,他们仍然会短暂地看到以前横向的视图控制器。

你可以这样解决:

首先在你的 viewDidLoad 添加:

override func viewDidLoad() {

    //This observer will call doneButtonClick method when done button is pressed.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

    var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    movieViewController = MPMoviePlayerViewController(contentURL: url)
}

之后添加这个方法:

func doneButtonClick(sender:NSNotification?){
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

这会强制将您的方向更改为纵向。

它工作正常。