2014-09-26 17 views
10

Bu kodu vardır:Swift: Manzara Modu Play Video zaman Tam Ekran

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

} 

sunucudan video oynatmak için çalışır, ama burada bir sorum var:

Ben portre moduna sahip olmak istiyorsanız Uygulama, kullanıcı yalnızca videoyu tam ekran modunda oynattığında manzaraya dönüştürebilir miyim? eğer öyleyse, bunu nasıl yapmalı?

Önceden teşekkürler.

+0

bunu nasıl buldun? – flakerimi

+0

@Flakerim: Hayır, henüz cevabı bulamadım ... Aynı probleminiz var mı? –

+1

Evet, bu yapılabilir. Program aracılığıyla bunu yapabilir, şu gönderiyi kontrol edebilirsiniz: http://stackoverflow.com/questions/25651969/setting-device-orientation-in-swift-ios – JaySH

cevap

0

Evet. MPMoviePlayercontroller tam ekran moduna geçtiğinde yönlendirmeyi zorlayarak değiştirebilirsiniz.

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

gözlemci kaldırmak unutmayın şöyle Sadece yönünü sizin viewDidLoad/viewWillAppear içine bildirim MPMoviePlayerWillEnterFullscreenNotification gözlemci ekleyip değiştirin.

Uygulama temsilcisi uygulamayı uygulayabilir: supportedInterfaceOrientationsForWindow: app'ın Info.plist dosyasındaki değerlerin yerine kullanılan bir UIInterfaceOrientationMask döndürür.

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

    } 
} 

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

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

İlgili konular