我提出一个控制器来播放视频:
[self presentModalViewController:movieController animated:YES];
并在完成播放时添加一个观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedPlayback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
-(void) movieFinishedPlayback:(NSNotification*)notification{
NSLog(@"........movieFinishedPlayback....... \n ");
[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
这是我的 VideoDetailViewController:
#import "VideoDetailViewController.h"
#import "PSStackedView.h"
#import "YunMaoIpadAppDelegate.h"
#define IpadAppDelegate ((YunMaoIpadAppDelegate *)[[UIApplication sharedApplication] delegate])
#define IphoneAppDelegate ((YunMaoIosAppDelegate *)[[UIApplication sharedApplication] delegate])
@interface VideoDetailViewController ()
@end
@implementation VideoDetailViewController
@synthesize video, moviePlayer, collectionsArray;
-(id)initWithVideo:(Video *)theVideo
{
self = [super initWithNibName:@"VideoDetailViewController" bundle:nil];
if (self) {
self.video = theVideo;
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[moviePlayer pause];
//[self.navigationController setNavigationBarHidden:false animated:animated];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated;
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[super viewDidDisappear:animated];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
}
- (void) viewDidAppear:(BOOL)animated
{
//[moviePlayer play];
//[DejalBezelActivityView removeViewAnimated:YES];
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(displayActivityView) withObject:self.moviePlayer.view afterDelay:0.1];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:video.link]];
[moviePlayer prepareToPlay];
//[moviePlayer setShouldAutoplay:NO];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if([moviePlayer respondsToSelector:@selector(useApplicationAudioSession)])
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSLog(@"iPhone ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 480.0f, 320.0f)];
//moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
}
else{
NSLog(@"iPad ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
else
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
NSLog(@"iPhone ios6.x");
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
}
if(result.height == 568)
{
//moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer.view setFrame:self.view.bounds];
}
}
else{
NSLog(@"ipad ios 6.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
//[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
- (BOOL)shouldAutorotate{
return NO;
}
/*
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationLandscapeRight;
}
*/
- (IBAction)displayActivityView
{
[DejalBezelActivityView activityViewForView:self.moviePlayer.view withLabel:@"节目正在下载中,请稍后..."].showNetworkActivityIndicator = NO;
}
- (void)removeActivityView;
{
[DejalBezelActivityView removeViewAnimated:YES];
[[self class] cancelPreviousPerformRequestsWithTarget:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end