video不会在iPhone 5.0模拟器上播放
Im新的xCode编程,我得到了这个代码从电子书教程播放mp4 xCode。
从一个button触发的function
(IBAction)playMovie:(id)sender { NSString *filepath = [[NSBundle mainBundle] pathForResource:@"videoSample" ofType:@"mp4"]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [self.view addSubview:moviePlayerController.view]; moviePlayerController.fullscreen=YES; [moviePlayerController play]; moviePlayerController.scalingMode = MPMovieScalingModeFill; }
从播放电影function称为
(void)moviePlaybackComplete:(NSNotification *)notification{ MPMoviePlayerController *moviePlayerController = [notification object]; [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [moviePlayerController.view removeFromSuperview]; }
我build立后没有错误和警告。 它只是输出一个空白的屏幕后,我点击触发playMoviefunction的button。 我混淆谷歌它,仍然不知道如何解决问题。
我使用xCode 4.2 iOS SDK 5.0
试试这个….用你的文件名称replace“example”,用文件typesreplace“m4v”,例如“mp4”(不是.mp4,不要把.mp4位放在文件名中)
-(IBAction)playVideo:(id)sender; { NSString *path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"m4v"]; MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; if (tmpMoviePlayViewController) { tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:tmpMoviePlayViewController animated:YES]; tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController]; [tmpMoviePlayViewController.moviePlayer play]; }
}
-(void)myMovieFinishedCallback:(NSNotification*)theNotification { MPMoviePlayerController *moviePlayer=[theNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [moviePlayer.view removeFromSuperview];
}
我不知道你是否还在寻找答案。 这是你可以尝试的 – 在头文件(.h)文件中声明MPMoviePlayerController (即使它成为一个成员variables)。
MPMoviePlayerController *moviePlayerController;
并将其添加到您的.m文件
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
如果它仍然不起作用,然后尝试将其声明为@属性。