Ios Swift:全屏时以横向模式播放视频
我有以下代码:Ios Swift:全屏时以横向模式播放视频,ios,swift,xcode,media-player,mpmovieplayercontroller,Ios,Swift,Xcode,Media Player,Mpmovieplayercontroller,我有以下代码: import UIKit import MediaPlayer class ViewController: UIViewController { var moviePlayer:MPMoviePlayerController! var bounds: CGRect = UIScreen.mainScreen().bounds override func viewDidLoad() { super.viewDidLoad()
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进入全屏模式时,您可以强制更改方向。只需将通知MPMoviePlayerwillerInterfullScreenNotification observer添加到您的ViewDiLoad/ViewWillDisplay中,并按如下方式更改方向
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:supportedInterfaceOrientionsforWindow:以返回UIInterfaceOrientationTask,该任务用于替代应用程序Info.plist中的值
class AppDelegate: UIResponder, UIApplicationDelegate {
.....
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.AllButUpsideDown;//UIInterfaceOrientationMask.All for iPad devices
}
}
参考:-
你是怎么找到的?@Flakerim:不,我还没有找到答案。。。你有同样的问题吗?是的,可以。您可以通过编程方式执行此操作,请查看以下帖子: