【问题标题】:MPMoviePlayerController not working on iOS8MPMoviePlayerController 在 iOS8 上不起作用
【发布时间】:2014-12-23 06:57:45
【问题描述】:
我正在尝试我在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];
}
}
谢谢。
【问题讨论】:
标签:
iphone
ios8
mpmovieplayercontroller
【解决方案1】:
最后。将它添加到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];
【解决方案2】:
在 .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