簡體   English   中英

Swift:在全屏模式下以橫向模式播放視頻

[英]Swift : Play Video in Landscape Mode When Fullscreen

我有這個代碼:

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

}

它適用於從服務器播放視頻,但我在這里有一個問題:

如果我想擁有縱向模式應用程序,只有當用戶以全屏模式播放視頻時,我仍然可以將其轉換為橫向嗎? 如果是的話,怎么做?

謝謝你。

是。 當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")
    }

不要忘記刪除觀察者。

應用程序委托可以實現應用程序: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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM