视频文件是http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 将其复制到应用程序包中后无法播放,然后运行:怎么了?
-(void)showvideo {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(mPMoviePlayerLoadStateDidChangeNotification) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[center addObserver:self selector:@selector(readytoplay) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];
NSBundle* b = NSBundle.mainBundle;
NSString* res = [b resourcePath];
NSString* file;
// file = @"sr.m4v";
// file = @"sample_iPod.m4v";
file = @"big_buck_bunny.mp4";
// file = @"xxx.mp4";
NSString* path = [res stringByAppendingPathComponent:file];
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:path]) {
NSLog(@"file exists!");
}
else {
NSLog(@"file does not exist!");
}
NSURL* url = [NSURL URLWithString:path];
NSLog(@"url: %@", url);
self.movieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// self.movieplayer = [MPMoviePlayerController new];
self.movieplayer.movieSourceType = MPMovieSourceTypeFile;
NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
[self.movieplayer prepareToPlay];
NSLog(@"self.movieplayer: %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}
-(void)readytoplay {
NSLog(@"readytoplay");
[self.movieplayer.view setFrame:self.view.bounds]; // player's frame must match parent's
[self.view addSubview:self.movieplayer.view];
NSLog(@"self.movieplayer: %@", self.movieplayer);
self.movieplayer.fullscreen = YES;
NSLog(@"self.movieplayer: %@", self.movieplayer);
[self.movieplayer play];
}
-(void)mPMoviePlayerLoadStateDidChangeNotification {
NSLog(@"mPMoviePlayerLoadStateDidChangeNotification %@, loadState: %d", self.movieplayer, self.movieplayer.loadState);
}
电影播放器属性:
@property (nonatomic, strong) MPMoviePlayerController* movieplayer;