MovieViewController的奇怪定位行为

时间:2015-06-12 10:03:20

标签: ios swift orientation mpmoviewcontroller

因此,在我的应用中,有MPMoviePlayerViewController允许用户观看视频。他们能够以任意方向观看MPMoviePlayerViewController中的视频,一旦完成并点击“完成”,他们将返回上一个视图控制器但仅限于纵向。问题是当按下“完成”按钮时,视图控制器会在返回到肖像之前在景观中短暂闪烁。这就是运行时的样子: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 当用户旋转到横向时,用户将被抛出电影播放器​​,并且在旋转为肖像之前,他们仍会在横向上简要地看到前一个视图控制器。

1 个答案:

答案 0 :(得分:1)

你可以这样解决:

首先在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")
}

将强制将您的方向改为肖像。

工作正常。