MPMoviePlayerController无法在iOS8上运行

时间:2014-12-23 06:57:46

标签: iphone ios8 mpmovieplayercontroller

我在iOS6期间尝试了一个项目。试图在iOS8上运行相同的视频并播放视频。还有什么我需要做的吗?

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"Mcdonald" ofType:@"mp4"];

    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];

    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}

- (void)playMovie:(NSNotification *)notification {
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"Movie is Ready to Play");
        [player play];
    }
}

感谢。

2 个答案:

答案 0 :(得分:0)

最后。将其添加到UIWindow可以解决我的问题。

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];

答案 1 :(得分:0)

在.h文件中初始化你的MPMoviePlayerController,如下所示:

@property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;

经过上述更改后,它可以正常工作。

示例代码:

@interface ViewController : UIViewController

@property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;

@end




@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

     NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"Mcdonald" ofType:@"mp4"];

     NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    _moviePlayerController.allowsAirPlay = NO;
    _moviePlayerController.shouldAutoplay = YES;
    [_moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
    _moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;


    [self.view addSubview:_moviePlayerController.view];
    _moviePlayerController.fullscreen = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];

}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

   }
- (void)playMovie:(NSNotification *)notification {
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"Movie is Ready to Play");
        [player play];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end