Подтвердить что ты не робот

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.
    }

}

он работает, чтобы воспроизводить видео с сервера, но у меня есть вопрос здесь:

Если я хочу иметь приложение в режиме портрета, могу ли я по-прежнему превращать его в пейзаж только тогда, когда пользователь воспроизводит видео в полноэкранном режиме? если да, то как это сделать?

спасибо раньше.

4b9b3361

Ответ 1

Да. Вы можете принудительно изменить ориентацию, когда MPMoviePlayercontroller переходит в полноэкранный режим. Просто добавьте уведомление MPMoviePlayerWillEnterFullscreenNotification observer в свой 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")
    }

не забудьте удалить наблюдателя.

Делегат приложения может реализовать приложение: supportedInterfaceOrientationsForWindow: вернуть UIInterfaceOrientationMask, который используется вместо значений из приложения Info.plist.

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

    }
}

Ссылка: - https://developer.apple.com/library/ios/qa/qa1688/_index.html

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