- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
NSString *movpath =[[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];
mpviemController =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movpath]];
[self.view addSubview:mpviemController.view];
mpviemController.view.hidden=YES;
MPMoviePlayerController *mp=[mpviemController moviePlayer];
[mp prepareToPlay];
UIImage *thumbImage=[mp thumbnailImageAtTime:5.0 timeOption: MPMovieTimeOptionNearestKeyFrame];
NSData *imagedata =UIImagePNGRepresentation(thumbImage);
[imagedata writeToFile:@"1.png" atomically:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivenotf:) name:MPMoviePlayerScalingModeDidChangeNotification object:mp];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivenotf:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
}
- (void) receivenotf:(NSNotification*)notification
{
NSLog(@"111xxx%@ \n %@",notification,[notification userInfo]);
mpviemController.view.hidden =YES;
/*
< add your code here >
MPMoviePlayerController* moviePlayerObj=[notification object];
etc.
*/
}
-(IBAction)play
{
CGRect frame = self.view.frame;
if (mpviemController.view.hidden ==YES) {
mpviemController.view.hidden =NO;
}
//[mpviemController moviePlayer].scalingMode = MPMovieScalingModeFill;
[[mpviemController moviePlayer] play];
}

感谢~
我也遇到了这个问题
我按照你的代码运行了一下,可是我只能看到视频,没有声音。这是为什么呢?(我的视频格式为.mov)
baoyifeng 声音关闭了没?另外可能是视频文件的问题。
是视频文件的问题。我用mp4就好了。
还有个问题:视频播放完后为什么有一个白屏闪一下,能否去掉(我把北京设置为red)?
附上我的代码:
- (void)receivenotf:(NSNotification *)notification{
//视频隐藏,并删去视频
mpviemController.view.hidden =YES;
[mpviemController.view removeFromSuperview];
//添加你的代码,实现你的效果,例如:
self.view.backgroundColor=[UIColor redColor];
}