我正在处理应用程序而我的应用程序仅支持纵向方式。
支持的方向:
现在我在viewController中使用MPMoviePlayerViewController。现在需要以横向和纵向模式显示视频。我怎样才能做到这一点。我用于MPMovieViewController的代码是:
-(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];
}
请给出一个线索。提前谢谢。
答案 0 :(得分:0)
我相信您需要实际监听来自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