Ios MPMoviePlayerViewController在所有方向
我正在开发一个应用程序,我的应用程序只支持纵向 支持的方向: 现在我在viewController中使用MPMoviePlayerViewController。现在需要以横向和纵向模式显示视频。我怎样才能做到这一点。我用于MPMovieViewController的代码是:Ios MPMoviePlayerViewController在所有方向,ios,mpmoviewcontroller,Ios,Mpmoviewcontroller,我正在开发一个应用程序,我的应用程序只支持纵向 支持的方向: 现在我在viewController中使用MPMoviePlayerViewController。现在需要以横向和纵向模式显示视频。我怎样才能做到这一点。我用于MPMovieViewController的代码是: -(void) playVideo { movieplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileUR
-(void) playVideo
{
movieplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp4"]]];
[[NSNotificationCenter defaultCenter] removeObserver:movieplayer
name:MPMoviePlayerPlaybackDidFinishNotification
object:movieplayer.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movieplayer.moviePlayer];
// Set the modal transition style of your choice
movieplayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
movieplayer.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
for(UIView* subV in movieplayer.moviePlayer.view.subviews) {
subV.backgroundColor = [UIColor clearColor];
}
[[movieplayer view] setBounds:CGRectMake(0, 0, 480, 320)];
CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2);
movieplayer.view.transform = transform;
movieplayer.moviePlayer.fullscreen=YES;
[self presentModalViewController:movieplayer animated:NO];
self.view addSubview:movieplayer.view];
[movieplayer.moviePlayer play];
}
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
MPMoviePlayerController *moviePlayer = [aNotification object];
moviePlayer.fullscreen = NO;
[movieplayer dismissModalViewControllerAnimated:NO];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
}
请给我一个线索。提前感谢。我认为您需要实际聆听来自UIDevice的旋转事件,以完成您正在尝试的操作。我已经创建了一个应用程序,它有一个锁定到纵向的UILabel和一个MPMoviePlayerViewController,当设备方向改变时,它会旋转。我使用了autolayout(因为这是我最熟悉的),但在iOS5风格的自动调整大小等方面应该很容易 运行这个,让我知道你是否在追求它 以下是我在AppDelegate.m中的整个应用程序:
#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MyViewController : UIViewController
@property (nonatomic, strong) MPMoviePlayerController *player;
@end
@implementation MyViewController
- (void)loadView
{
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"]];
self.player.scalingMode = MPMovieScalingModeAspectFit;
[self.player play];
UIView *playerView = self.player.view;
[self.view addSubview:playerView];
playerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[playerView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(playerView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[playerView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(playerView)]];
UILabel *wontRotate = [[UILabel alloc] init];
wontRotate.backgroundColor = [UIColor clearColor];
wontRotate.textColor = [UIColor whiteColor];
wontRotate.text = @"This stays in portrait";
wontRotate.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:wontRotate];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case UIDeviceOrientationLandscapeLeft:
self.player.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
break;
case UIDeviceOrientationLandscapeRight:
self.player.view.transform = CGAffineTransformMakeRotation(3 * M_PI / 2);
break;
case UIDeviceOrientationPortraitUpsideDown:
self.player.view.transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIDeviceOrientationPortrait:
self.player.view.transform = CGAffineTransformMakeRotation(2 * M_PI);
break;
default:
NSLog(@"Unknown orientation: %d", orientation);
}
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[MyViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
#导入“AppDelegate.h”
#进口
@接口MyViewController:UIViewController
@属性(非原子,强)MPMoviePlayerController*播放器;
@结束
@MyViewController的实现
-(void)负荷视图
{
[超级加载视图];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(DeviceOrientationIDChange:)名称:UIDeviceOrientationIDChangeNotification对象:nil];
[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications];
self.player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@]http://techslides.com/demos/sample-videos/small.mp4"]];
self.player.scalingMode=MPMovieScalingModeAspectFit;
[self.player play];
UIView*playerView=self.player.view;
[self.view addSubview:playerView];
playerView.translatesAutoresizingMaskIntoConstraints=否;
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“H:|[playerView]|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(playerView)];
[self.view addConstraints:[nsLayoutConstraintsWithVisualFormat:@“V:|[playerView]|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(playerView)];
UILabel*wontRotate=[[UILabel alloc]init];
wontRotate.backgroundColor=[UIColor clearColor];
wontRotate.textColor=[UIColor-whiteColor];
wontRotate.text=@“这保持在纵向”;
wontRotate.translatesAutoresizingMaskIntoConstraints=否;
[self.view addSubview:wontRotate];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate
属性:nsLayoutAttributeCenter
关系人:NSLayoutRelationEqual
toItem:self.view
属性:nsLayoutAttributeCenter
乘数:1.0
常数:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate
属性:nsLayoutAttributeCenter
关系人:NSLayoutRelationEqual
toItem:self.view
属性:nsLayoutAttributeCenter
乘数:1.0
常数:0.0]];
}
-(无效)DeviceOrientationIDChange:(NSNotification*)通知{
UIDeviceOrientation方向=[[UIDevice currentDevice]方向];
开关(方向){
案例UIDeviceOrientation和scapeLeft:
self.player.view.transform=CGAffineTransformMakeRotation(M_PI/2);
打破
案例UIDeviceOrientation和scapeRight:
self.player.view.transform=CGAffineTransformMakeRotation(3*M_PI/2);
打破
案例UIDeviceOrientation肖像向上向下:
self.player.view.transform=CGAffineTransformMakeRotation(M_PI);
打破
案例UIDeviceOrientation纵向:
self.player.view.transform=CGAffineTransformMakeRotation(2*M_-PI);
打破
违约:
NSLog(@“未知方向:%d”,方向);
}
}
-(整数)支持的接口方向
{
返回UIInterfaceOrientationMaskPortrait;
}
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController=[[MyViewController alloc]init];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
返回YES;
}
@结束
我已经试过了。但它使所有的ViewController都处于纵向和横向模式,但我只想旋转MPMovieViewController。好的,我想我现在明白你的意思了。我已经按照你说的更新了答案