2015-06-12 61 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 –