Swift:在全屏模式下以横向模式播放视频

Sai*_*son 10 xcode mpmovieplayercontroller media-player ios swift

我有这个代码:

import UIKit
import MediaPlayer

class ViewController: UIViewController {
    var moviePlayer:MPMoviePlayerController!
    var bounds: CGRect = UIScreen.mainScreen().bounds

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var width:CGFloat = bounds.size.width
        var height = (width / 16) * 9

        var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
        moviePlayer = MPMoviePlayerController(contentURL: url)
        moviePlayer.view.frame = CGRect(x: 0, y: 40, width: width, height: height)
        self.view.addSubview(moviePlayer.view)
        moviePlayer.fullscreen = true
        moviePlayer.controlStyle = MPMovieControlStyle.Embedded
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
Run Code Online (Sandbox Code Playgroud)

它适用于从服务器播放视频,但我在这里有一个问题:

如果我想拥有纵向模式应用程序,只有当用户以全屏模式播放视频时,我仍然可以将其转换为横向吗?如果是的话,怎么做?

谢谢你.

kar*_*agu 0

是的。当 MPMoviePlayercontroller 进入全屏模式时,您可以强制更改方向。只需将通知 MPMoviePlayerWillEnterFullscreenNotification 观察者添加到 viewdidload/viewwillappear 中并更改方向,如下所示

     override func viewDidLoad() {
            super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoMPMoviePlayerWillEnterFullscreen:", name: MPMoviePlayerWillEnterFullscreenNotification, object: nil);
//change orientation to portrait when user exits the full screen


 NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoMPMoviePlayerWillExitFullscreenNotification:", name: MPMoviePlayerWillExitFullscreenNotification, object: nil);

    }

   func  videoMPMoviePlayerWillEnterFullscreen(notification:NSNotification)
    {
        let value = UIInterfaceOrientation.LandscapeLeft.rawValue ;// UIInterfaceOrientation.LandscapeRight.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }



  func  videoMPMoviePlayerWillExitFullscreenNotification(notification:NSNotification)
    {
        let value = UIInterfaceOrientation.Portrait.rawValue ;// UIInterfaceOrientation.LandscapeRight.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }
Run Code Online (Sandbox Code Playgroud)

不要忘记删除观察者。

应用程序委托可以实现 application:supportedInterfaceOrientationsForWindow: 返回一个 UIInterfaceOrientationMask,用于代替应用程序 Info.plist 中的值。

class AppDelegate: UIResponder, UIApplicationDelegate {
.....
   func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.AllButUpsideDown;//UIInterfaceOrientationMask.All for iPad devices

    }
}
Run Code Online (Sandbox Code Playgroud)

参考:- https://developer.apple.com/library/ios/qa/qa1688/_index.html

https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html