斯威夫特:播放视频在风景模式下全屏
问题描述:
我有这样的代码:斯威夫特:播放视频在风景模式下全屏
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")
}
不要忘记删除观察者。
应用程序委托可以实现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
你找到它怎么样? – flakerimi 2015-01-03 22:49:39
@Flakerim:不,我还没有找到答案...你有同样的问题吗? – 2015-01-22 08:32:04
是的,可以做到。你可以通过编程来实现,查看这篇文章:http://stackoverflow.com/questions/25651969/setting-device-orientation-in-swift-ios – JaySH 2015-05-20 15:54:35