gpt4 book ai didi

ios - swift : Play Video in Landscape Mode When Fullscreen

转载 作者:IT王子 更新时间:2023-10-29 05:38:27 24 4
gpt4 key购买 nike

我有这段代码:

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

不要忘记移除观察者。

应用程序委托(delegate)可以实现 application: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

关于ios - swift : Play Video in Landscape Mode When Fullscreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com