2014-09-26 4 views
10

Ich habe diesen Code:Swift: Video Wiedergabe im Querformat-Modus Wenn Vollbild

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

} 

es funktioniert Video von Server zu spielen, aber ich habe eine Frage hier:

wenn ich will Portrait-Modus haben Anwendung, kann ich es immer noch in Landschaft verwandeln, nur wenn Benutzer das Video im Vollbildmodus abspielen? Wenn ja, wie geht das?

danke vor.

+0

fanden Sie es wie? – flakerimi

+0

@Flakerim: Nein, ich habe die Antwort noch nicht gefunden ... hast du das gleiche Problem? –

+1

Ja, das kann getan werden. Sie können das programmgesteuert tun, überprüfen Sie diesen Beitrag: http://stackoverflow.com/questions/25651969/setting-device-orientation-in-swift-ios – JaySH

Antwort

0

Ja. Sie können die Ausrichtung gewaltsam ändern, wenn MPMoviePlayercontroller in den Vollbildmodus wechselt. Fügen Sie einfach die Benachrichtigung MPMoviePlayerWillEnterFullscreenNotification Beobachter in Ihrem viewDidLoad/ViewWillAppear und die Ausrichtung ändern, wie

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

nicht den Beobachter entfernen vergessen folgt.

Der Anwendungsdelegat kann eine Anwendung implementieren: supportedInterfaceOrientationsForWindow: um eine UIInterfaceOrientationMask zurückzugeben, die anstelle der Werte aus der Info.plist der App verwendet wird.

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

    } 
} 

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

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

Verwandte Themen