10

I have this code :

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

}

it works to play video from server, but I have a question here :

if I want to have portrait mode application, can I still turn it into landscape only when user play the video in fullscreen mode? if so, how to do that?

thanks before.

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
Saint Robson
  • 5,475
  • 18
  • 71
  • 118

1 Answers1

0

Yes. You can forcefully change the orientation when MPMoviePlayercontroller goes into fullscreen mode. Just add the notification MPMoviePlayerWillEnterFullscreenNotification observer into your viewdidload/viewwillappear and change the orientation as follows

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

don't forget to remove the observer.

The application delegate may implement application:supportedInterfaceOrientationsForWindow: to return a UIInterfaceOrientationMask that is used in place of the values from the app's Info.plist.

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

    }
}

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

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

karthikPrabhu Alagu
  • 3,371
  • 1
  • 21
  • 25