您有几种方法可供您使用。也许最简单和最可控的就是子类化MPMoviePlayerViewController
.
首先让您的应用支持所有界面方向(在 中app-info.plist
)。然后使用适用于 iOS 6 和更早版本操作系统的方法限制您的列表视图(假设它是):MyListViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationMaskPortrait;
}
- (NSUInteger) supportedInterfaceOrientations {
return(UIInterfaceOrientationMaskPortrait);
}
- (BOOL) shouldAutorotate {
return FALSE;
}
现在创建一个派生自 的新的 Objective C 类MPMoviePlayerViewController
,称为MyMoviePlayerViewController
. 这是MyMoviePlayerViewController.h
:
#import <MediaPlayer/MediaPlayer.h>
@interface MyMoviePlayerViewController : MPMoviePlayerViewController
@end
这是MyMoviePlayerViewController.m
:
#import "MyMoviePlayerViewController.h"
@interface MyMoviePlayerViewController ()
@end
@implementation MyMoviePlayerViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSUInteger) supportedInterfaceOrientations {
return(UIInterfaceOrientationMaskAll);
}
- (BOOL) shouldAutorotate {
return TRUE;
}
-(id)initWithContentURL:(NSURL *)contentURL {
UIGraphicsBeginImageContext(CGSizeMake(1,1));
self = [super initWithContentURL:contentURL];
UIGraphicsEndImageContext();
if (self) {
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:[self moviePlayer]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[self moviePlayer]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self moviePlayer] setFullscreen:YES animated:NO];
[self moviePlayer].controlStyle = MPMovieControlStyleDefault;
}
- (void)movieFinishedCallback:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
然后,添加到MyListViewController.h
:
#import "MyMoviePlayerViewController.h"
并且,在 中MyListViewController.m
,使用:
MyMoviePlayerViewController *player =[[MyMoviePlayerViewController alloc]
initWithContentURL:URLWithString:[[main_data objectAtIndex:indexPath.row]
objectForKey:@"url"]]];
[self presentViewController:player animated:YES completion:nil];
显然,您可以调整设置(例如动画样式、要显示的控件...)以满足您的特定需求。