0

我提出一个控制器来播放视频:

  [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
4

2 回答 2

1

这对我有用。

[self dismissViewControllerAnimated:NO completion:^{
    [self.view removeFromSuperview];
}];

希望能帮助到你!

于 2012-12-25T14:00:23.713 回答
0

在关闭movieplayer之前,请从superview中[moviePlayer stop];删除movieplayer.view。之后尝试解散。那可能会解决问题。

于 2012-12-25T09:26:41.973 回答