0

所以在我的应用程序中有一个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 当用户旋转到横向时,用户会被赶出电影播放器​​,并且在旋转到纵向之前,他们仍然会短暂地看到之前的横向视图控制器.

4

1 回答 1

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")
}

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

它工作正常。

于 2015-06-12T10:22:16.920 回答