2015-06-12 25 views
0

所以在我的应用程序中有一个MPMoviePlayerViewController它允许用户查看视频。他们能够以任何方向在MPMoviePlayerViewController中观看视频,并且一旦完成并点击“完成”,它们将返回到先前的视图控制器,但仅以纵向显示。问题是,当按下“完成”按钮时,视图控制器会短暂地在风景中闪烁,然后像应该那样返回到肖像。这是它看起来运行时,这样的:http://1drv.ms/1RSqUUDMovieViewController的奇怪的方向行为

我的代码附:

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

可以解决这样说:在那之后添加此方法

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

这将强行改变你的方向

首先在你viewDidLoad添加此肖像。

它工作正常。

+0

我刚刚尝试过,但每当我尝试观看风景中的视频时,用户都将从视频播放器中移除,并在旋转至肖像之前在风景中显示视图控制器... @Dharmesh_Khani – dwinnbrown

+0

我应该替换名为“旋转“与你的功能?我的代码在这里:http://txt.do/nt1s – dwinnbrown

+0

这是完整的代码:http://pastebin.com/VLkdmXjx –